Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public static void Compress(string strPath)
        {
            DateTime current;
            string dstFile = "";// "C:\\temp\\compressed-file.gzip";
            FileStream fsIn = null;
            FileStream fsOut = null;
            GZipStream gzip = null;
            byte[] buffer;
            int count = 0;
            try
            {
                current = DateTime.Now;
                dstFile = Application.StartupPath + "\\" + "Request" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".zip"; // may use own extension

                fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
                gzip = new GZipStream(fsOut, CompressionMode.Compress, true);
                fsIn = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                buffer = new byte[fsIn.Length];
                count = fsIn.Read(buffer, 0, buffer.Length);
                fsIn.Close();
                fsIn = null;

                // compress to the destination file
                gzip.Write(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                // handle or display the error
                System.Diagnostics.Debug.Assert(false, ex.ToString());
            }
            finally
            {
                if (gzip != null)
                {
                    gzip.Close();
                    gzip = null;
                }
                if (fsOut != null)
                {
                    fsOut.Close();
                    fsOut = null;
                }
                if (fsIn != null)
                {
                    fsIn.Close();
                    fsIn = null;
                }
            }
        }


i m using this function,it creates successfully zip file,but when i m opening it showing error-(Invalid or Corrupted file) plz help me
Posted
Comments
Sergey Alexandrovich Kryukov 19-Oct-12 1:48am    
What makes you thinking it's ZIP.
--SA

Alternative solution:

If you want to work with ZIP, not GZip, you can use #ziplib:
http://www.icsharpcode.net/opensource/sharpziplib/[^].

Another option is using SevenZipSharp, a .NET wrapper of the famous 7-Zip:
http://en.wikipedia.org/wiki/7-Zip[^],
http://sevenzipsharp.codeplex.com/[^].

Both ZIP libraries are open-source.

—SA
 
Share this answer
 
Here is my guess: you are creating a file using GZip algorithm, but you name your file as *.zip. ZIP is a different algorithm.

Please see:
http://en.wikipedia.org/wiki/Gzip[^],
http://en.wikipedia.org/wiki/ZIP_%28file_format%29[^],
http://en.wikipedia.org/wiki/List_of_archive_formats[^].

As you are trying to use ZIP file naming schema, the Windows Shell can use "file type association" to launch some default application used to load the file; in your case, named as *.zip. I have no idea what do you mean by "I'm opening it", but chances are, your Shell opens some application which supports ZIP files (WinZip, or the packer embedded in File Manager like Explorer or something else — you should know better). This application may assume that the file is a ZIP file, while its context is GZip-compressed data. No wonder, from the standpoint of ZIP format, this file is considered corrupted.

Try to change the name to *.GZip and try again. It should work.

—SA
 
Share this answer
 
v2
Comments
StackQ 19-Oct-12 3:30am    
Is there any way without using third party tool,so plz tell me.I don't want to use any other dll except default.
Sergey Alexandrovich Kryukov 19-Oct-12 12:22pm    
I already told you -- do the same, only change your file name. 3-rd party tool is required for ZIP, this is my alternative solution. If there is not ZIP in .NET FCL, how can you make is "without using third party tool"?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900