简体   繁体   中英

Using 7-zip in powershell

I want to extract csv files from csv.gz zip file. I have downloaded 7-zip and trying to use the same with the below code:

$7zprogram = "C:\Program Files\7-Zip\7z.exe"
$sourcefile = "C:\Users\Mitesh\Desktop\file.csv.gz"
$destination = "C:\Users\Mitesh\Desktop\"

& $7zprogram e $sourcefile "-o$destination"

This isn't working and I am getting an error as "the term "C:\Program Files\7-Zip\7z.exe" is not recognized as a cmdlet, function, ........".

What can be the issue? Can somebody help me to correct the code here if something is missing.

Try with this:

$command = "$installRoot\bin\7za.exe" 
$cmdparams = "x `"$Source`" -o`"$destination`" -bb3 $OverWriteSwitch >`"$env:Temp\Decompression-7z-$global:time.log`"" 

Invoke-Expression "& $command $cmdparams" 
    if ($LASTEXITCODE -gt 0) {  
        write-log "Error decompressing $Source in $Destination - check log file $env:Temp\Decompression-7z-$global:time.log"
    }

} else 
{
    write-log  "Successfully decompressed $source into $Destination"
}

Here's an alternative answer using .NET for gzip files only . I think this should work on PowerShell 2.0 and upwards. I've added a function for converting to and from GZip for convenience. There is an optional -RemoveInputFile switch that I have left in if you want to tidy up the file you passed through once it has been added to the .gz file or vice-versa. During testing it is useful to ensure that the file doesn't exist when going back the other way. Be wary about using this before testing with sample files . Anyway, here are the functions.

function ConvertTo-Gzip {
    Param (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [System.IO.FileSystemInfo]
        $InputObject,

        [Parameter(Mandatory=$false)]
        [switch]
        $RemoveInputFile
    )
    Process {
        
        if ($InputObject -is [System.IO.DirectoryInfo]) {
            throw "You cannot gunzip a directory!"
        }
        else {
            # Create Filestream for new gz archive
            [System.IO.FileStream]$CompressedFileStream = [System.IO.File]::Create($InputObject.FullName + ".gz")
            $GZipStream = [System.IO.Compression.GZipStream]::new($CompressedFileStream, [System.IO.Compression.CompressionMode]::Compress)

            # Copy file to GZip filestream
            $Filestream = $InputObject.OpenRead()
            $FileStream.CopyTo($GZipStream)

            # Cleanup filestreams
            $FileStream.Dispose()
            $Filestream = $null
            $GZipStream.Dispose()
            $GZipStream = $null

            # Remove the initial file if requested.
            if ($PSBoundParameters.ContainsKey('RemoveInputFile')) {
                $InputObject.Delete()
            }
        }
    }
}

function ConvertFrom-Gzip {
    Param (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateScript({(Get-Item $_).Name.EndsWith(".gz")})]
        [System.IO.FileInfo]
        $InputObject,

        [Parameter(Mandatory=$false)]
        [switch]
        $RemoveInputFile
    )
    Process {
        # Create a new file and open a filestream for it
        $NewFilename = $InputObject.FullName.Remove($InputObject.FullName.Length - $InputObject.Extension.Length)
        $DecompressedFileStream = [System.IO.File]::Create($NewFilename)

        # Open the compressed file and copy the file to the decompressed stream
        $CompressedFileStream = $InputObject.OpenRead()
        $GZipStream = [System.IO.Compression.GZipStream]::new($CompressedFileStream, [System.IO.Compression.CompressionMode]::Decompress)
        $GZipStream.CopyTo($DecompressedFileStream)

        # Cleanup
        $DecompressedFileStream.Dispose()
        $GZipStream.Dispose()
        $CompressedFileStream.Dispose()
        $DecompressedFileStream,$GZipStream,$CompressedFileStream = $null

        # Remove the initial file if requested.
        if ($PSBoundParameters.ContainsKey('RemoveInputFile')) {
            $InputObject.Delete()
        }
    }
}

Sample usage:

Get-Item C:\Users\Ash\Desktop\test1.txt | ConvertTo-Gzip -RemoveInputFile
Get-Item C:\Users\Ash\Desktop\test1.txt.gz | ConvertFrom-Gzip -RemoveInputFile

Sounds like Powershell cant find 7z.exe.

You can test this theory with the following version of your code:

$7zprogram = "C:\Program Files\7-Zip\7z.exe"
$sourcefile = "C:\Users\Mitesh\Desktop\file.csv.gz"
$destination = "C:\Users\Mitesh\Desktop\"


if(Test-Path -Path $7zprogram){
    & $7zprogram e $sourcefile "-o$destination"
}else{
    Write-Host "7z.exe does not exist";
}

Im assuming it will output 7z.exe does not exist . If so, make sure you have 7zip installed, and its filename and path are correct (ive tested your command and it is syntactically correct/should work). Your problem is almost certainly this line: $7zprogram = "C:\Program Files\7-Zip\7z.exe"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM