-2

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 process, should it be needed.

Eiketsu
  • 203
  • 1
  • 2
  • 14

3 Answers3

1

.NET Framework provides a System.IO.Compression.DeflateStream class in the base library; it implements the DEFLATE compression algorithm. A zlib stream is just zlib magic bytes followed by content compressed with the DEFLATE algorithm.

oops, I gave instructions for "zlib" streams, which are more raw than gzip. A gzip has a directory and other stuff, in addition to the deflate-compressed content.

For gzip, use the framework's System.IO.Compression.GZipStream class, as Mike_G's answer shows.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Any chance you could expand on that a bit? It seems to be along the lines of what I'm looking for, but it's a bit...cryptic for someone as green as I still am. Sorry. :-/ – Eiketsu Sep 30 '14 at 13:40
1

This is what I use for gzip-ing an object:

byte[] initialBytes = //object serialized to bytes
byte[] compressedBytes;

        using (MemoryStream stream = new MemoryStream(initialBytes))
        {
            using (MemoryStream output = new MemoryStream())
            {
                using (GZipStream zipper = new GZipStream(output, CompressionMode.Compress))
                {
                    Pump(stream, zipper);
                }

                compressedBytes = output.ToArray();
            }
        }

 return compressedBytes;


//pump method

internal static void Pump(Stream input, Stream output)
    {
        byte[] bytes = new byte[4096];
        int n;
        while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
        {
            output.Write(bytes, 0, n);
        }
    }

if I need to do a file:

byte[] result;

        using(FileStream file = //some filestream I created)
        {
            file.Position = 0;

            using (MemoryStream output = new MemoryStream())
            {
                using (GZipStream zipper = new GZipStream(output, CompressionMode.Compress))
                {
                    Pump(file, zipper);

                }

                result = output.ToArray();
            }
        }



        return result;
Mike_G
  • 16,237
  • 14
  • 70
  • 101
0

This is what I wound up resorting to in this case.

    string jobDirVar = [FilePath];
    byte[] bytesToCompress = File.ReadAllBytes(jobDirVar);
    byte[] decompressedBytes = new byte[bytesToCompress.Length];
    using (FileStream fileToCompress = File.Create(FilePath))
        {
            using (GZipStream compressionStream = new GZipStream(fileToCompress, CompressionMode.Compress))
            {
                compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length);
            }
        }

        using (FileStream fileToDecompress = File.Open(FilePath, FileMode.Open))
        {
            using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
            {
                decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
            }
        }

It's not the prettiest bit of coding, but it seems to have provided me with what I was after. If anyone else has anything that might streamline things a bit, I'd love to still hear it, though!

Eiketsu
  • 203
  • 1
  • 2
  • 14