Questions tagged [gzipstream]

GZipStream is a .NET 2.0+ class for compression and decompression using gzip format.

GZipStream is a .NET 2.0+ class for compression and decompression using gzip format., while including a redundancy check value for detecting data corruption. Compressed GZipStream object written to a .gz file can be decompressed using WinZip, 7zip, etc, but GZipStream does NOT provide functionality for adding or extracting files from .zip archives.

The compression functionality in DeflateStream and GZipStream is exposed as a stream. Data is read in on a byte-by-byte basis, so it is not possible to perform multiple passes to determine the best method for compressing entire files or large blocks of data. The DeflateStream and GZipStream classes are best used on uncompressed sources of data. If the source data is already compressed, using these classes may actually increase the size of the stream.

//Path to directory of files to compress and decompress.
string dirpath = @"c:\users\public\reports";

DirectoryInfo di = new DirectoryInfo(dirpath);

// Compress the directory's files.
foreach (FileInfo fi in di.GetFiles()){
    Compress(fi);
}

public static void Compress(FileInfo fi){
  // Get the stream of the source file.
  using (FileStream inFile = fi.OpenRead())
  {
    // Prevent compressing hidden and already compressed files.
    if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
       != FileAttributes.Hidden & fi.Extension != ".gz")
    {
      // Create the compressed file.
      using (FileStream outFile=File.Create(fi.FullName + ".gz"))
      {
          using (GZipStream Compress=new GZipStream(outFile,CompressionMode.Compress))
          {
              // Copy the source file into the compression stream.
              inFile.CopyTo(Compress);
              Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                  fi.Name, fi.Length.ToString(), outFile.Length.ToString());
           }
       }
     }
  } 

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

Do not confuse with that is a Java class.

319 questions
-1
votes
1 answer

GZip Magic Number Missing After AES Encryption

I am compressing a file using GZip and then encrypting using AES. When I step through the code, I see that the magic number (1f 8b) is present in the intermediate compressed unencrypted data and then the file is encrypted. When I go to decrypt the…
Derek W
  • 9,708
  • 5
  • 58
  • 67
-1
votes
1 answer

Decompressing using GZipStream

I have a zipped up file within a resource and am attempting to decompress this zip file. However I keep getting this exception...? The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. byte[] zipFile =…
-2
votes
3 answers

Using Streamreader to Convert File Contents to String Array, then Compressing

EDITED: I needed to convert the contents of a text file to a string array, write them to another - untyped - file, and compress the created file into a gzip. Below I've supplied the answer I was able to discover, as well as the decompression…
Eiketsu
  • 203
  • 1
  • 2
  • 14
-4
votes
1 answer

c# gzipstream decompression is more like depression

why can't I get this code here to work? I want to call this on a byte array that was previously compressed....anyway, it just returns an empty string... public static string FromGZipToString( this byte[] source ) { using(…
Timmerz
  • 6,090
  • 5
  • 36
  • 49
1 2 3
21
22