none
Zip folder in C# RRS feed

  • Question

  • Hi guys, Can I have an example of how to Zip a folder in C# ?
    By the way, I do not see namespace ICSharpCode. I downloaded ICSharpCode.SharpZipLib.dll but I do not know where to copy that DLL file. What do I need to do to see this namespace?
    And do you have link for that MSDN example for compress folder, because I read all MSDN but I couldn't find anything.
    Saturday, July 27, 2019 12:26 PM

Answers

  • With .NET 4.5, Creating a zip file becomes incredibly easy. No third-party libraries will be required.  Don't forget to add a reference to System.IO.Compression.FileSystem and a using statement for System.IO.Compression

    string startPath = @"c:\example\start";
    string zipPath = @"c:\example\result.zip";
    string extractPath = @"c:\example\extract";
    
    ZipFile.CreateFromDirectory(startPath, zipPath);
    ZipFile.ExtractToDirectory(zipPath, extractPath);
    Saturday, July 27, 2019 12:27 PM
  • There is a ZipPackage class in the System.IO.Packaging namespace which is built into .NET 3, 3.5, and 4.0.

    http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

    Following code uses a third-party Ultimate Zip Component

    using ComponentPro.Compression;
    using ComponentPro.IO;
    
    ...
    
    // Create a new instance.
    Zip zip = new Zip();
    // Create a new zip file.
    zip.Create("test.zip");
    
    zip.Add(@"D:\Temp\Abc"); // Add entire D:\Temp\Abc folder to the archive.
    
    // Add all files and subdirectories from 'c:\test' to the archive.
    zip.AddFiles(@"c:\test");
    // Add all files and subdirectories from 'c:\my folder' to the archive.
    zip.AddFiles(@"c:\my folder", "");
    // Add all files and subdirectories from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2", "22");
    // Add all .dat files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2", "22", "*.dat");
    // Or simply use this to add all .dat files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2\*.dat", "22");
    // Add *.dat and *.exe files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2\*.dat;*.exe", "22");
    
    TransferOptions opt = new TransferOptions();
    // Donot add empty directories.
    opt.CreateEmptyDirectories = false;
    zip.AddFiles(@"c:\abc", "/", opt);
    
    // Close the zip file.
    zip.Close();
    Using a free, LGPL licensed SharpZipLib is a common alternative.


    Saturday, July 27, 2019 12:30 PM

All replies

  • With .NET 4.5, Creating a zip file becomes incredibly easy. No third-party libraries will be required.  Don't forget to add a reference to System.IO.Compression.FileSystem and a using statement for System.IO.Compression

    string startPath = @"c:\example\start";
    string zipPath = @"c:\example\result.zip";
    string extractPath = @"c:\example\extract";
    
    ZipFile.CreateFromDirectory(startPath, zipPath);
    ZipFile.ExtractToDirectory(zipPath, extractPath);
    Saturday, July 27, 2019 12:27 PM
  • There is a ZipPackage class in the System.IO.Packaging namespace which is built into .NET 3, 3.5, and 4.0.

    http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

    Following code uses a third-party Ultimate Zip Component

    using ComponentPro.Compression;
    using ComponentPro.IO;
    
    ...
    
    // Create a new instance.
    Zip zip = new Zip();
    // Create a new zip file.
    zip.Create("test.zip");
    
    zip.Add(@"D:\Temp\Abc"); // Add entire D:\Temp\Abc folder to the archive.
    
    // Add all files and subdirectories from 'c:\test' to the archive.
    zip.AddFiles(@"c:\test");
    // Add all files and subdirectories from 'c:\my folder' to the archive.
    zip.AddFiles(@"c:\my folder", "");
    // Add all files and subdirectories from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2", "22");
    // Add all .dat files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2", "22", "*.dat");
    // Or simply use this to add all .dat files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2\*.dat", "22");
    // Add *.dat and *.exe files from 'c:\my folder' to '22' folder within the archive.
    zip.AddFiles(@"c:\my folder2\*.dat;*.exe", "22");
    
    TransferOptions opt = new TransferOptions();
    // Donot add empty directories.
    opt.CreateEmptyDirectories = false;
    zip.AddFiles(@"c:\abc", "/", opt);
    
    // Close the zip file.
    zip.Close();
    Using a free, LGPL licensed SharpZipLib is a common alternative.


    Saturday, July 27, 2019 12:30 PM