-3

I need to create a task to download a file from a website that is gzipped and then ungzip it and copy it to various folders on the server. I don't really know what is the best approach to achieve this. Is there any suggestions/best practices?

I'm thinking of create a bash script to do it but I don't know how to unzip the file, and even here I don't know if I should use the normal shell or powershell.

1 Answer 1

2

@danielgek - Here is what you can use from a PowerShell standpoint:

Syntax to download a file from a site:

$source = "http://example.com/file.zip"

$destination = "c:\file.zip"

Invoke-WebRequest $source -OutFile $destination

and here's a function to unzip a file.

function Extract-Zip($file, $destination) {

$shell = new-object -com shell.application

$zip = $shell.NameSpace($file)

foreach($item in $zip.items()) {

$shell.Namespace($destination).copyhere($item) }

}

You can use the function in your script like so:

Extract-Zip –File “C:\file.zip” –Destination “C:\destination”

EDIT: Here's a function you can use for GZip files:

Function DeGZip-File{
 Param(
    $infile,
    $outfile = ($infile -replace '\.gz$','')
    )

 $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
 $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
 $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
 $buffer = New-Object byte[](1024)
 while($true){
  $read = $gzipstream.Read($buffer, 0, 1024)
  if ($read -le 0){break}
  $output.Write($buffer, 0, $read)
  }

 $gzipStream.Close()
 $output.Close()
 $input.Close()
}

$infile='C:\Temp\DECfpc1new.csv.gz'
$outfile='c:\temp\DECfpc1new.csv'

DeGZip-File $infile $outfile`
2
  • thanks for the answer but its a gzip file, power shell suport it ?
    – danielgek
    Sep 30, 2014 at 16:34
  • 1
    @danielgek - I added a function for extracting gzip files to the original answer.
    – bentek
    Sep 30, 2014 at 19:03

Not the answer you're looking for? Browse other questions tagged .