tags:

views:

112

answers:

4

Hi,

I want to zip one "CSV" file in to Zip file using C#.Net. Below i have written some code for create Zip file , using this code i am able to create zip file but after creating "Data1.zip" file extract manually means extracted file extension should be ".csv" but it is not coming.

        FileStream sourceFile = File.OpenRead(@"C:\Users\Rav\Desktop\rData1.csv");
        FileStream destFile = File.Create(@"C:\Users\Rav\Desktop\Data1.zip");

        GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress,false);

        try
        {
            int theByte = sourceFile.ReadByte();
            while (theByte != -1)
            {
                compStream.WriteByte((byte)theByte);
                theByte = sourceFile.ReadByte();
            }
        }
        finally
        {
            compStream.Dispose();
        }
A: 

i worked on creation of zip file and its working fine for me. you can find code in below link:

http://dotnetacademy.blogspot.com/2008/10/compress-files-foleder-in-vbnet.html

Rajesh Rolen- DotNet Developer
Sorry, but you code sample is terrible.
Restuta
Haven't you heard about http://code.google.com/p/syntaxhighlighter/ ? Having a blog about programming and not using such tools is a crime against your readers.
Alexander
@Restuta: but its working. You can edit it according to you requirements
Rajesh Rolen- DotNet Developer
@Alexander: its very old post.. will update it. thanks
Rajesh Rolen- DotNet Developer
The author asked about C#. You gave him a VB-Java mix which I don't even want to figure out if it works because the code is unreadable.
Alexander
@Fielding: its easy to make fun of others but its hard to try to help others. and i am trying my best to help others. may be its look and feel is not good.. but these contents are very helpful for me and my followers.
Rajesh Rolen- DotNet Developer
Gabe
@Gabe, really interesting. Thanks for the information.
Alexander
+4  A: 

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

This is gzip compression, and apparently it only compresses a stream, which when decompressed takes the name of the archive without the .gz extension. I don't know if I'm right here though. You might as well experiment with the code from MSDN, see if it works.

I used ZipLib for zip compression. It also supports Bz2, which is a good compression algorithm.

Alexander
I dont see why this post gets downvoted.
atamanroman
+2  A: 

Use one of these libraries: http://www.icsharpcode.net/opensource/sharpziplib/ http://dotnetzip.codeplex.com/

I prefer #ziplib, but both are well documented and widely spread.

atamanroman
A: 

Use ICSharpCode.SharpZipLib(you can download it) and do the following

       private void CreateZipFile(string l_sFolderToZip)
       {
            FastZip z = new FastZip();
            z.CreateEmptyDirectories = true;
            z.CreateZip(l_sFolderToZip + ".zip", l_sFolderToZip, true, "");

            if (Directory.Exists(l_sFolderToZip))
                Directory.Delete(l_sFolderToZip, true);   

      }



        private void ExtractFromZip(string l_sFolderToExtract)
        {
            string l_sZipPath ="ur folder path" + ".zip";
            string l_sDestPath = "ur location" + l_sFolderToExtract;

            FastZip z = new FastZip();
            z.CreateEmptyDirectories = true;
            z.ExtractZip(l_sZipPath, l_sDestPath, "");

            if (File.Exists(l_sZipPath))
                File.Delete(l_sZipPath);
        }

Hope it helps...

srivatsayb