我正在尝试创建一个可以即时压缩任何文件的脚本。为此,我使用了 DLL SharpZipLib。但无法运行包。以下是我在互联网上找到的代码,我已将其用作脚本任务控件中的示例。
===========================================
使用系统;使用 System.Data;使用 Microsoft.SqlServer.Dts.Runtime;使用 System.Windows.Forms;使用 ICSharpCode.SharpZipLib.Checksums;使用 ICSharpCode.SharpZipLib.Zip;使用 ICSharpCode.SharpZipLib.Zip.Compression.Streams;使用 ICSharpCode.SharpZipLib.Core;使用 System.IO;
公共无效主要(){
CreateSample("D:\\TestZipResult\\", "D:\\TestZipTarget\\");
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
// 压缩指定文件夹中的文件,并在磁盘上创建一个名为 outPathname 的 zip 文件。//
public void CreateSample(string outPathname, string folderName)
{
FileStream fsOut = File.Create(outPathname);
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
// This setting will strip the leading part of the folder path in the entries, to
// make the entries relative to the starting folder.
// To include the full path for each entry up to the drive root, assign folderOffset = 0.
int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
CompressFolder(folderName, zipStream, folderOffset);
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close();
}
// Recurses down the folder structure
//
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.
// 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);
}
}
===========================================
以下是我收到的错误,尽管对 DLL 的引用已正确添加。
================================================ 错误:System.Reflection.TargetInvocationException:调用目标引发了异常。 ---> System.IO.FileNotFoundException:无法加载文件或程序集“ICSharpCode.SharpZipLib,Version=0.86.0.518,Culture=neutral,PublicKeyToken=1b03e6acf1164f73”或其依赖项之一。系统找不到指定的文件。文件名:“ICSharpCode.SharpZipLib,Version=0.86.0.518,Culture=neutral,PublicKeyToken=1b03e6acf1164f73”
===========================================
如果有人能帮助我解决这个问题,我将非常感激。
问候,F. Ahmed
答案1
我不确定如何使用 SharpZipLib 执行此操作 - 看起来在查找程序集时出现了问题。您是否尝试过 GAC 该程序集?
也就是说,对于 SSIS 压缩任务来说,这应该是一件容易的事http://www.nsoftware.com/ssis/。