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
0
votes
1 answer

C# - Files compressed rename operation or compress without GZipStream

I've a problematic situation where I should compress an file and so rename it to what the user choose + ".zip"/".rar"/".tar.gz"/".tar". About compress itself is everything Ok, but when I try to rename the file with something like File.Move() or…
0
votes
0 answers

Base64encoded GZipStream is not matching windows

I have the following csv file: https://dl.dropboxusercontent.com/u/8518063/ShareX/2016/03/simple.csv I use windows explorer to "send to compressed zip folder" and get this file: https://dl.dropboxusercontent.com/u/8518063/ShareX/2016/03/simple.zip I…
LearningJrDev
  • 911
  • 2
  • 8
  • 27
0
votes
1 answer

How does GZipStream determine the size of compressed data

I have two files (I use 7zip): n1.txt.gz and n2.txt.gz. Then I combine them to the file n12.txt.gz by command prompt: type n1.txt.gz > n12.txt.gz type n2.txt.gz >> n12.txt.gz If I decompress the file n12.txt.gz by 7zip, I will get combined…
Andrei
  • 17
  • 7
0
votes
1 answer

GZipStream doesn't read the whole file

Using GZipStream to open downloaded gzip files and get the xml file inside. Problem is that sometimes the whole xml file isn't extracted by my code: private static string Unzip(string fileToUnzip, string format) { string…
Louisa
  • 552
  • 1
  • 9
  • 22
0
votes
2 answers

Decompressing GZIP stream

I am trying to decompress a GZipped string which is part of response from a webservice. The string that I have…
Zachi Shtain
  • 826
  • 1
  • 13
  • 31
0
votes
2 answers

Gzip only after a threshold reached?

I have a requirement to archive all the data used to build a report everyday. I compress most of the the data using gzip, as some of the datasets can be very large (10mb+). I write each individual protobuf graph to a file. I also whitelist a fixed…
Michael B
  • 7,512
  • 3
  • 31
  • 57
0
votes
1 answer

Add multiple in memory XmlDocument to a GZipStream or to a zip file

I'm developing an ASP.NET MVC 5 app with .NET Framework 4.5.1 and C#. I have an action method that must return one or more xml. These XML files will be in memory, I will generate each XML for an Entity Framework data model. At the end I will have…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
0
votes
3 answers

Prevent GZipStream/DeflateStream from trying to consume more than the compressed data

I have a file that could have been created something like this: stream.Write(headerBytes, 0, headerBytes.Count); using (var gz = new GZipStream(stream, Compress, leaveOpen: true); { gz.Write(otherBytes, 0,…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
0
votes
1 answer

How to write binary data from GZipStream after decompression to a file as binary data in C# .NET 2.0

How to write binary data from GZipStream after decompression to a file as binary data in C# .NET 2.0: FileStream fileStream = new FileStream("compressed_file.zip", FileMode.Open, FileAccess.Read); GZipStream compressionStream = new…
Computer User
  • 2,839
  • 4
  • 47
  • 69
0
votes
1 answer

How to compress a file using GZipStream in C# in .NET 2.0

I want to compress a file that has binary data and save the compressed data in another file: FileStream fileStream = new FileStream("compressed_file.bin", FileMode.Create, FileAccess.Write); GZipStream compressionStream = new GZipStream(fileStream,…
Computer User
  • 2,839
  • 4
  • 47
  • 69
0
votes
0 answers

GZipStream & gzencode

i want use GZip compression algorithme in PHP & C#. Code in C# byte[] bs1 = File.ReadAllBytes(inName); memoryStream1 = new MemoryStream(); gZipStream = new GZipStream(memoryStream1, CompressionMode.Compress); gZipStream.Write(bs1, 0,…
EPIC FAIL
  • 1
  • 1
  • 3
0
votes
2 answers

Gzip Decompression Error

I want to decompress a zip file. The code that I used is so simple. I could not understand why I' m getting this error; The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. public static void…
Anil Kocabiyik
  • 1,178
  • 6
  • 17
  • 47
0
votes
1 answer

How to get type and number of .zip contents

How can i get the content names of a zipped folder in C# i.e. name of files and folders inside the compressed folder? I want to decompress the zip by using GZipStream only. thanks, kapil
Jaqen H'ghar
  • 1,839
  • 7
  • 37
  • 66
0
votes
0 answers

How to get an uncoded string from a base64 encoded gzipped text

Text I am reading from a XML, which is suppose to be a string with base64 encoded and Gzip compressed. I am following the below steps: string text = childNodes.Item(i).InnerText.Trim(); byte[] compressed = Convert.FromBase64String(text); byte[]…
Anand
  • 823
  • 1
  • 10
  • 19
0
votes
1 answer

How to compressed Large String with gzip in android

I try to compressed large string in android Like this: try { String str = "MyLarge String"; ByteArrayOutputStream rstBao = new ByteArrayOutputStream(str.length()); GZIPOutputStream zos = new GZIPOutputStream(rstBao); …