Question

i 'm .net developer. i want to Zip all files and make a one zip file with this technique.

ZipFile multipleFilesAsZipFile = new ZipFile();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".zip");
                Response.ContentType = "application/zip";
                for (int i = 0; i < filename.Length; i++)
                {
                    string filePath = Server.MapPath("~/PostFiles/" + filename[i]);
                    multipleFilesAsZipFile.AddFile(filePath, string.Empty);
                }
multipleFilesAsZipFile.Save(Response.OutputStream);

how ever for making this Zip i use third party library Ionic.

all files are ziped successfully but not extracted to client desktop. is there problem with my code. or this library that i'm using has been expired.

Is there free full version .net compatible library to zip all files.

Was it helpful?

Solution

Use SharpZipLib:

Nuget Package

Install-Package SharpZipLib    

OR Download here

http://www.icsharpcode.net/OpenSource/SharpZipLib/

Snippet from examples:

private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {

        FileInfo fi = new FileInfo(filename);

        string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
        entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
        ZipEntry newEntry = new ZipEntry(entryName);
        newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

        // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
                // A password on the ZipOutputStream is required if using AES.
        //   newEntry.AESKeySize = 256;

        // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
        // you need to do one of the following: Specify UseZip64.Off, or set the Size.
        // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
        // but the zip will be in Zip64 format which not all utilities can understand.
        //   zipStream.UseZip64 = UseZip64.Off;
        newEntry.Size = fi.Length;

        zipStream.PutNextEntry(newEntry);

        // Zip the file in buffered chunks
        // the "using" will close the stream even if an exception occurs
        byte[ ] buffer = new byte[4096];
        using (FileStream streamReader = File.OpenRead(filename)) {
            StreamUtils.Copy(streamReader, zipStream, buffer);
        }
        zipStream.CloseEntry();
    }
    string[ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
        CompressFolder(folder, zipStream, folderOffset);
    }
}

Taken from : https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples

Works awesome!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top