Kees de Wit Kees de Wit - 3 years ago 108
C# Question

GZipStream delivers zero byte file

I am using DotNetZip's GZipStream to zip a file. The problem I have is that the resulting file is empty. I tried flushing/closing streams, but without result. Anyone knows what I do wrong:

using (var outputStream = new FileStream(path + fileName + ".gz", FileMode.Create, FileAccess.Write, FileShare.None))
{
using (var zipStream = new GZipStream(outputStream, CompressionMode.Compress))
{
using (var inputStream = new FileStream(path + fileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
await inputStream.CopyToAsync(zipStream);
}
}
}

Answer Source

Works fine here; do you have a fully reproducible example, perhaps based on this one?

Results:

dummy.txt:6492 bytes
Waiting for completion (don't do this in real code, ever)...
Complete
dummy.txt.gz:512 bytes

Code:

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;

static class P
{
    static void Main()
    {

        File.WriteAllLines("dummy.txt",
            Enumerable.Range(1, 200).Select(i => "this is some contents: line " + i));

        WriteSize("dummy.txt");

        var t = Task.Run(() => DoTheThing(Environment.CurrentDirectory + "\\", "dummy.txt"));
        Console.WriteLine("Waiting for completion (don't do this in real code, ever)...");
        t.Wait();
        Console.WriteLine("Complete");
        WriteSize("dummy.txt.gz");
    }

    private static void WriteSize(string path)
    {
        var file = new FileInfo(path);
        Console.WriteLine(path + ":" + file.Length + " bytes");
    }

    async static Task DoTheThing(string path, string fileName)
    {
        using (var outputStream = new FileStream(path + fileName + ".gz", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using (var zipStream = new GZipStream(outputStream, CompressionMode.Compress))
            {
                using (var inputStream = new FileStream(path + fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    await inputStream.CopyToAsync(zipStream);
                }
            }
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download