Добрый день!
Использую для создание архива библиотеку SharpZipLib.
Не получается в цикл передать список файлов и создать архив.
На данный момент он просто удаляет файлы.
Вот мой код:
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;
}
}
}
Как с помощью этой библиотеки корректно создать архив необходимых файлов? Спасибо!
Создаёшь файл архива:
ZipFile zipFile = new ZipFile(@"c:\temp\existing.zip");
Добавляешь твои файлы:
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 e.g. "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();
}
Сделано на основе этого https://github.com/icsharpcode/SharpZipLib/wiki/Updating . Работоспособность кода не проверял.
Сборка персонального компьютера от Artline: умный выбор для современных пользователей