Good day!
I use to create an archive library SharpZipLib.
I can’t transfer a list of files and create an archive.
At the moment it simply deletes the files.
Here is my code:

using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using System.IO.Compression; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Core; public void Main() { var myDate = DateTime.Now; var startOfMonth = new DateTime(myDate.Year, myDate.Month, 1); string rootFolderPath = @"E:\Download"; string filesToDelete = @"*T_D_FD_DRYUA*"; string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete); if (DateTime.Now!= startOfMonth) { foreach (string file in fileList) { //System.Diagnostics.Debug.WriteLine(file + "will be deleted"); //System.IO.File.Delete(file); Dts.TaskResult = (int)ScriptResults.Success; } } } 

How to use this library to correctly create an archive of necessary files? Thank!

  • Try the library Ionic.zip , - GooliveR
  • I generally do not go out to create such a cycle. Regardless of the library. - Andrey Obukhovsky

1 answer 1

Create an archive file:

 ZipFile zipFile = new ZipFile(@"c:\temp\existing.zip"); 

Add your files:

 public void Add() { ... foreach (string file in fileList) { UpdateExistingZip(file); } } public void UpdateExistingZip(string file) { // Must call BeginUpdate to start, and CommitUpdate at the end. zipFile.BeginUpdate(); //zipFile.Password = "whatever"; // Only if a password is wanted on the new entry // The "Add()" method will add or overwrite as necessary. // When the optional entryName parameter is omitted, the entry will be named // with the full folder path and without the drive eg "temp/folder/test1.txt". // //zipFile.Add(@"c:\temp\folder\test1.txt"); zipFile.Add(file); // Specify the entryName parameter to modify the name as it appears in the zip. // //zipFile.Add(@"c:\temp\folder\test2.txt", "test2.txt"); // Continue calling .Add until finished. // Both CommitUpdate and Close must be called. zipFile.CommitUpdate(); zipFile.Close(); } 

Made on the basis of this https://github.com/icsharpcode/SharpZipLib/wiki/Updating . The performance of the code is not checked.