tags:

views:

239

answers:

4

I have a problem when opening a zip file. I am using this code to zip the file:

public static string Zip_File(string soruce , string target)
        {
            try
            {
                byte[] bufferWrite;               
                using (FileStream fsSource = new FileStream(soruce, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    bufferWrite = new byte[fsSource.Length];
                    fsSource.Read(bufferWrite, 0, bufferWrite.Length);
                    using (FileStream fsDest = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (GZipStream gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true))
                        {
                            gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
                            bufferWrite = null;
                            fsSource.Close();
                            gzCompressed.Close();
                            fsDest.Close();
                        }
                    }
                }
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

When I call this function am receiving "success" message, but I can't able to open the zip file.

ZipFiles.Zip_File(@"C:\Documents and Settings\ccspl\Desktop\IntegrityDVR.mdb", @"C:\Documents and Settings\ccspl\Desktop\a.zip")

This is my function call code:

the compressed(folder) is invalid or corrupted ...> this is error message which I have received

+7  A: 

GZipStream does not create .zip files. It creates .gz files. If you need to create .zip files, you should use something like SharpZipLib.

Mehrdad Afshari
no Mehrdad ... my prob not yet solved... it's giving the same error msg
RV
RV: Of course **it doesn't create .ZIP files**. You need a program like 7-Zip to open `.gz` files.
Mehrdad Afshari
Cheeso: I suggested `Flush` as a way to start troubleshooting. That was before I understood the OP didn't want Gzip at all; he wants Zip.
Mehrdad Afshari
+1  A: 

but, wait a minute, GZipStream doesn't create zip file, it creates gzip files as I know, Zipping files using GZipStream should help

ArsenMkrt
The article you cited, at http://www.geekpedia.com/tutorial190_Zipping-files-using-GZipStream.html , is bogus. It repeatedly claims that GZipSream can be used to zip files, or produce a .Zip archive. Not true.
Cheeso
+1  A: 

Why not use SharpZipLib? It makes this a lot easier.

Druid
+1  A: 

sample code for DotNetZip, an open source zip library.

public static string ZipFile(String source, String target)
{
    try 
    {
        using (ZipFile zip = new ZipFile()
        {
            zip.AddFile(source);
            zip.Save(target);
        }
        return "success";
    }
    catch {}
    return "failure";
}
Cheeso
+1 for a good and very useful library reference, and thanks for correction
ArsenMkrt