0

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 decompressed original files (n1.txt + n2.txt). But if I use this code

public static void Decompress2(String fileSource, String fileDestination, int buffsize)
{
 using (var fsInput = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
 {
  using (var fsOutput = new FileStream(fileDestination, FileMode.Create, FileAccess.Write))
  {
   using (var gzipStream = new GZipStream(fsInput, CompressionMode.Decompress))
   {
    var buffer = new Byte[buffsize];
    int h;
    while ((h = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
    {
     fsOutput.Write(buffer, 0, h);
    }
   }
  }
 }
}

I will get just decompressed first part of n12.txt.gz, i.e. decompressed n1.txt.

Why does GZipStream stop after first part of combined file? And how does 7zip decompress the whole file?

Andrei
  • 17
  • 7

1 Answers1

0

GZipStream does not implement methods to decompress multiple files from one stream.

Try using a library which handles ZIP archives such as DotNetZip.

If you absolutelly wanto to use GZipStream you could search the input stream for the gzip header and then feed GZipStream only the portions of the stream belonging to each file.

John Caprez
  • 393
  • 2
  • 10
  • Ok. Thanks. However, I've read gzip format. If I write the size of each portions to gzip header I'll read the portions and GZipStream will be decompress them correctly. But _7zip_ decompress my _n12.txt.gz_ file, and the file have no the size of portions. Do you know how _7zip_ do it? – Andrei Mar 20 '16 at 17:55
  • No I don't know how 7zip does it but I think they might just assume your archive is broken and try to make the best of it. Have a look at [how ZIP archives are structured](https://en.wikipedia.org/wiki/Zip_%28file_format%29#Structure) and you will see that your method misses the file directory necessary to identify where each file starts. – John Caprez Mar 21 '16 at 18:18