This topic demonstrates how to add files and folders to an archive. To add files and folders to an archive, you need to perform the following steps:
- Specify a folder path whose files will be added to the archive.
- Retrieve a reference to a new or existing archive file using the Zip class.
- Call AddFile or AddFiles methods to copy or move existing files to the archive.
See the following example more more details:
C#
// Create a new instance. Zip zip = new Zip(); // Create a new zip file. zip.Create("test.zip"); // Set password zip.EncryptionAlgorithm = EncryptionAlgorithm.PkzipClassic; zip.Password = "password"; // 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"); // Close the zip file. zip.Close();
VB.NET
' Create a new instance. Dim zip As New Zip() ' Create a new zip file. zip.Create("test.zip") ' Set password zip.EncryptionAlgorithm = EncryptionAlgorithm.PkzipClassic zip.Password = "password" ' 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") ' Close the zip file. zip.Close()
Posted via web from ComponentSoft.net Ultimate ZIP Component's Blog
Recent Comments