-1

I am writing a code to compress a ZIP file in C# using the built in .NET library:

using System.IO.Compression; using System.IO;

But, however, when the compression finishes, the code outputs an invalid zip file. It seems like somewhere down the line in the code, the file either did not write properly or close fully. I have used dispose and close to release the resources.

public bool CompressFile(FileInfo theFile)
        {
           
            StringBuilder compressSuccess = new StringBuilder();
            FileStream sourceFile = File.OpenRead(theFile.FullName.ToString());
            FileStream destinationFile = File.Create(theFile.FullName + ".zip");

            byte[] buffer = new byte[sourceFile.Length];
            sourceFile.Read(buffer, 0, buffer.Length);

            using (GZipStream output = new GZipStream(destinationFile,
                CompressionMode.Compress, true))
            {

                output.Write(buffer, 0, buffer.Length);
        
                


            }

            sourceFile.Dispose();
            destinationFile.Dispose();
            sourceFile.Close();
            destinationFile.Close();
           
            return true;
        }

What would I been doing wrong? Is it because I am forcing an extension ".zip"?

E_net4
  • 27,810
  • 13
  • 101
  • 139

2 Answers2

1

Following the link suggested by FrankJames, this is an example of code to create a zip file:

        var zipFile = @"e:\temp\outputFile.zip";
        var theFile = @"e:\temp\sourceFile.txt";
        using (var zipToCreate = new FileStream(zipFile, FileMode.Create))
        {
            using (var archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create))
            {
                var fileEntry = archive.CreateEntry("FileNameInsideTheZip.txt");
                using (var sourceStream = File.OpenRead(theFile))
                using (var destStream = fileEntry.Open())
                {
                    var buffer = new byte[sourceStream.Length];
                    sourceStream.Read(buffer, 0, buffer.Length);
                    destStream.Write(buffer, 0, buffer.Length);
                    destStream.Flush();
                }
            }
        }
PiGi78
  • 415
  • 2
  • 6
0

I see you never used the Flush method that force to write to the stream.

I rewrite your code using a better style (using instead of the dispose). Try it and let us know:

    public bool CompressFile(FileInfo theFile)
    {
        using (var sourceStream = File.OpenRead(theFile.FullName))
        using (var destStream = File.Create(theFile.FullName + ".zip"))
        {
            var buffer = new byte[sourceStream.Length];
            sourceStream.Read(buffer, 0, buffer.Length);
            using (var zipStream = new GZipStream(destStream, CompressionMode.Compress, true))
            {
                zipStream.Write(buffer, 0, buffer.Length);
                zipStream.Flush();
            }
            destStream.Flush();
        }
        return true;
    }
PiGi78
  • 415
  • 2
  • 6
  • Hi Pigi, after trying the solution, it seems that I am getting the same error. Windows can't open the file as it's invalid. – user1769881 Dec 20 '20 at 21:07
  • I only suggested you how to write the code you posted in the right way (using + flush methods). If you need to zip a file, FrankJames gave you the link where Microsoft teach you how to do it. – PiGi78 Dec 20 '20 at 21:13