我正在尝试创建一个脚本,它可以在动态中对任何文件进行压缩。为此,我使用了一个DLL SharpZipLib。但无法运行软件包。下面是我在互联网上找到的代码,我在脚本任务控件中作为示例使用了这些代码。
===========================================
使用系统;使用ICSharpCode.SharpZipLib.Zip.Compression.Streams;;使用Microsoft.SqlServer.Dts.Runtime;使用System.Windows.Forms;使用ICSharpCode.SharpZipLib.Checksums;使用ICSharpCode.SharpZipLib.Zip;使用ICSharpCode.SharpZipLib.Zip;使用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
发布于 2011-11-16 16:42:56
我不知道如何使用SharpZipLib来完成这个任务--看起来找到程序集存在一个问题。你试过去参加大会吗?
也就是说,对于http://www.nsoftware.com/ssis/上的SSIS压缩任务来说,这应该是一个简单的任务。
https://serverfault.com/questions/320461
复制相似问题