Guest User

Untitled

a guest
Aug 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. # Example working code to decompress a file with an extension of "tar.gz". ie Uncompress (expand) a sample
  2. # file such as: filename.tar.gz.
  3.  
  4. # Admittedly, there are better ways of writing this code more succinctly but using the layout given, it will
  5. # remind me of how this task is done. I don't expand 'tar.gz' files very often so I needed a writing style
  6. # which would be a good reminder of how to do this in the future.
  7.  
  8. #
  9. $srcfile = 'C:\Test\filename.tar.gz';
  10. $destfile = 'C:\Test\sampledata.dat';
  11.  
  12. $path = $srcfile;
  13. $mode = [System.IO.FileMode]::Open;
  14. $access = [System.IO.FileAccess]::Read;
  15. $share = [System.IO.FileShare]::Read;
  16. $fis = New-Object -typeName 'System.IO.FileStream' -ArgumentList `
  17. $path, $mode, $access, $share;
  18.  
  19. $path = $destfile;
  20. $mode = [System.IO.FileMode]::Create;
  21. $access = [System.IO.FileAccess]::Write;
  22. $share = [System.IO.FileShare]::None;
  23. $fos = New-Object -typeName 'System.IO.FileStream' -ArgumentList `
  24. $path, $mode, $access, $share;
  25.  
  26. $stream = $fis;
  27. $mode = [System.IO.Compression.CompressionMode]::Decompress;
  28. $gzipStream = New-Object -typeName 'System.IO.Compression.GzipStream' -ArgumentList `
  29. $stream, $mode;
  30.  
  31. $databuffer = New-Object Byte[] 4KB;
  32. $bytesRead = -1;
  33.  
  34. do {
  35. $bytesRead = $gzipstream.Read($databuffer, 0, $databuffer.Length)
  36. $fos.Write($databuffer, 0, $bytesRead)
  37. } while ($bytesRead -gt 0)
  38.  
  39. $gzipStream.Dispose();
  40. $fos.Dispose();
  41. $fis.Dispose();
  42.  
  43. # Have a quick look at the files we've been dealing with.
  44. Get-ChildItem $srcfile, $destfile;
Add Comment
Please, Sign In to add comment