1

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, I am receiving "success" message, but I can't open the zip file. This is my function call code:

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

This is the error message I receive:

the compressed(folder) is invalid or corrupted

CarenRose
  • 1,266
  • 1
  • 12
  • 24
RV.
  • 2,782
  • 8
  • 39
  • 51

4 Answers4

8

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
  • 414,610
  • 91
  • 852
  • 789
1

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

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • 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 Mar 07 '10 at 03:44
1

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

Druid
  • 6,423
  • 4
  • 41
  • 56
1

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
  • 189,189
  • 101
  • 473
  • 713