我试图通过从服务器获取路径并使用vjslib.dll库来创建一个包含一些文件的zip文件夹
代码在ZipOutputStream outf = new ZipOutputStream(new FileOutputStream(outFilename));
我得到的错误 vjslib.dll中出现'System.TypeInitializationException'类型的第一次机会异常
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using java.util.zip;
using java.io;
namespace s1.csproj
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void Main()
{
try
{
ZipFiles();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
public void ZipFiles()
{
string xlsFileName = Dts.Variables["User::ZipFileMask"].Value.ToString();
string ZipFileName = Dts.Variables["User::ZipFileName"].Value.ToString();
string ZipFilePath = Dts.Variables["User::ZipFilePath"].Value.ToString();
//Delete the zip file
if (System.IO.File.Exists(ZipFilePath + ZipFileName))
System.IO.File.Delete(ZipFilePath + ZipFileName);
string FilePath = Dts.Variables["User::ZipFilePath"].Value.ToString() ;
// These are the files to include in the ZIP file
string[] FileNames = Directory.GetFiles(FilePath, xlsFileName);
// Create a buffer for reading the files
sbyte[] buff = new sbyte[1024];
try
{
// Create the ZIP file
String outFilename = ZipFilePath + ZipFileName;
ZipOutputStream outf = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress the files
for (int i = 0; i < FileNames.Length; i++)
{
string strsourcefile = FileNames[i];
FileInputStream inx = new FileInputStream(strsourcefile);
string filename = Path.GetFileName(FileNames[i]);
string strzipentry = filename;
ZipEntry ze = new ZipEntry(strzipentry);
outf.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
while ((len = inx.read(buff)) > 0)
{
outf.write(buff, 0, len);
}
outf.closeEntry();
inx.close();
}
outf.close();
}
catch
{
throw;
}
}
}
}
答案 0 :(得分:2)
这是我尝试使用几种方法。一个使用GZipStream,另一个使用ZipArchive。这两个都在System.IO.Compression命名空间中。 ZipArchive需要引用System.IO.Compression.FileSystem程序集以获取其示例中所需的扩展方法。请参阅代码中的注释。
using System;
using System.IO;
using System.IO.Compression;
namespace SOTesting
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
try
{
// I couldn't get the gz to open afterward
ZipFilesG(); // GZipStream attempt
//** !! Must add reference to System.IO.Compression.FileSystem for this to work
ZipFilesZ(); // ZipArchive attempt; this worked well for me
Dts.TaskResult = (int)ScriptResults.Success;
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void ZipFilesG()
{
// You may need to adjust these a bit
string xlsFileName = "*.xlsx"; // Dts.Variables["User::ZipFileMask"].Value.ToString();
string zipFileName = "SSIS.gz"; // Dts.Variables["User::ZipFileName"].Value.ToString();
string zipFilePath = @"C:\TEMP\SSIS"; // Dts.Variables["User::ZipFilePath"].Value.ToString();
string fullFilePath = zipFilePath + "\\" + zipFileName;
MessageBox.Show($"fullFilePath: {fullFilePath}");
//Delete the zip file
if (File.Exists(fullFilePath)) File.Delete(fullFilePath);
DirectoryInfo filePath = new DirectoryInfo(zipFilePath);
try
{
FileStream compressedFileStream = new FileStream(fullFilePath, FileMode.Append);
GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress);
foreach (FileInfo fileToCompress in filePath.GetFiles(xlsFileName))
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
originalFileStream.CopyTo(compressionStream);
}
}
compressionStream.Close();
compressedFileStream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public void ZipFilesZ()
{
// You may need to adjust these a bit
string xlsFileName = "*.xlsx"; // Dts.Variables["User::ZipFileMask"].Value.ToString();
string zipFileName = "SSIS.zip"; // Dts.Variables["User::ZipFileName"].Value.ToString();
string zipFilePath = @"C:\TEMP\SSIS"; // Dts.Variables["User::ZipFilePath"].Value.ToString();
string fullFilePath = zipFilePath + "\\" + zipFileName;
//Delete the zip file
if (File.Exists(fullFilePath)) File.Delete(fullFilePath);
DirectoryInfo filePath = new DirectoryInfo(zipFilePath);
try
{
foreach (FileInfo fileToCompress in filePath.GetFiles(xlsFileName))
{
using (ZipArchive archive = ZipFile.Open(fullFilePath, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(fileToCompress.FullName, fileToCompress.Name);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
祝你好运!
答案 1 :(得分:0)
我不确定你为什么要包含一些java的引用但是确定...
答案 2 :(得分:0)
您需要的只是here。正如其他人所提到的那样 - 你的方法似乎与Java不同!?