0

I want to compress a Excel file to .zip or .cap extension. The code Used to do that it is compressing the file but that zip file can't be unzip. while unzip that i am getting the error file file corrupted or can't be unzip. The code I am using:

    static public bool CompressFile(string file, string outputFile)

      {

        try
        {

            using (var inFile = File.OpenRead(file))
            {

                using (var outFile = File.Create(outputFile))
                {

                    using (var compress = new GZipStream(outFile, CompressionMode.Compress, false))
                    {

                        byte[] buffer = new byte[inFile.Length];


                        int read = inFile.Read(buffer, 0, buffer.Length);

                        while (read > 0)
                        {
                            compress.Write(buffer, 0, read);
                            read = inFile.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
            return true;
        }
        catch (IOException ex)
        {
       MessageBox.Show(string.Format("Error compressing file: {0}", ex.Message));
            return false;
        }
    }

Even i go some link to get the proper solution. But nothing is workout.I need some suggestion to get the proper solution. Any answer please.

Krunal Patil
  • 3,666
  • 5
  • 22
  • 28
HomeWork
  • 263
  • 1
  • 4
  • 14
  • GZip and ZIP are not the same thing. – Frédéric Hamidi Jun 05 '14 at 09:04
  • the code is working to compress to .zip. but i am not able to extract that zip file. while extracting it is showing no file to extract... – HomeWork Jun 05 '14 at 09:06
  • @FrédéricHamidi: i am doing for .zip. – HomeWork Jun 05 '14 at 09:08
  • You're using a `GZipStream`, which outputs GZip format (`.gz`), not ZIP format. – Frédéric Hamidi Jun 05 '14 at 09:09
  • @FrédéricHamidi: I am new to it. let me try for .gz, but i want the sol for .zip – HomeWork Jun 05 '14 at 09:10
  • @FrédéricHamidi: .gz all so not working for that code. if i am giving . .gz extension it is generating some "dll" kind of file which can't be extracted. any code u have to make .zip. – HomeWork Jun 05 '14 at 09:15
  • Worth looking: http://msdn.microsoft.com/en-us/library/vstudio/system.io.compression.zipfile – Konstantin Smolyakov Jun 05 '14 at 09:18
  • @KonstantinSmolyakov: I had through that link. it is using System.IO.Compression.FileSystem.dll. Where i will get that Dll. as i am using asp.net 3.5. in add reference that Dll is not present. – HomeWork Jun 05 '14 at 09:25
  • @HomeWork Then it's easier to use some open source library like http://www.nuget.org/packages/DotNetZip/ – Konstantin Smolyakov Jun 05 '14 at 09:27
  • @KonstantinSmolyakov: Thanks but i can't install anything without prior notice. can you suggest some other sol. – HomeWork Jun 05 '14 at 09:32
  • @HomeWork Other solution: http://stackoverflow.com/questions/6386113/using-system-io-packaging-to-generate-a-zip-file – Konstantin Smolyakov Jun 05 '14 at 09:38
  • @KonstantinSmolyakov: no that link all so not give me the sol. please anything suggest – HomeWork Jun 05 '14 at 09:48
  • Have you tried the code in the exmaple for the [GZipStream class](http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx). Also for your info, xslx format is already compressed. – NoviceProgrammer Jun 05 '14 at 09:49
  • 1
    @HomeWork Did you add reference to WindowsBase.dll to use System.IO.Packaging namespace? – Konstantin Smolyakov Jun 05 '14 at 09:55
  • 1
    Zip compression is built into the `.xslx` format. – Sam Axe Jun 05 '14 at 10:13
  • @HomeWork compressing an xlsx file is pointless because it's already Zip compressed. You'll probably increase the file's size instead of reducing it, as the `.zip` file will contain the original data with minimal compression, plus the headers required by the zip format – Panagiotis Kanavos Jun 05 '14 at 10:46
  • @PanagiotisKanavos:ya but as my .xls file contains huge amount of data it is not able to attached in the mail. so it is needed to be compressed – HomeWork Jun 05 '14 at 10:58
  • `xls` is a different format than `xslx`. `xls` is uncompressed, `xlsx` is zip-compressed – Panagiotis Kanavos Jun 05 '14 at 11:29
  • @KonstantinSmolyakov:Hi as per your code i did the and it is compressing and working fin. but it is creating a blank XML file inside the zip folder with my .xls file. can you please help me out to remove that XML file. – HomeWork Oct 09 '14 at 13:43

1 Answers1

1

This code uses the SharpZipLib library and will compress files that can be uncompressed no problems

    private void Zip()
    {
        string output = @"C:\TEMP\test.zip";
        string input = @"C:\TEMP\test.xlsx";

        using (var zipStream = new ZipOutputStream(System.IO.File.Create(output)))
        {
            zipStream.SetLevel(9);
            var buffer = new byte[4096];
            var entry = new ZipEntry(Path.GetFileName(input));
            zipStream.PutNextEntry(entry);

            using (FileStream fs = System.IO.File.OpenRead(input))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, sourceBytes);
                } while (sourceBytes > 0);
            }

            zipStream.Finish();
            zipStream.Close();
        }
    }
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • Yes I can read comments, but firstly I answered the question that was asked. Secondly when I did this I did see a reduction in the size of file from 8.50kb to 5.25kb. So yes they maybe compressed, but that doesn't mean they can't be compressed more! – matt_lethargic Jun 05 '14 at 10:49