我在SSIS中使用Microsoft Visual C#2012的脚本任务。
我想解压20160423.zip里面有一个测试文件夹,里面的test文件夹有插入,删除和更新文件夹,每个文件夹里都有.dat文件:
20160423.zip:
我想这样解压缩:
这是层次结构
脚本任务看起来像
public void Main()
{
string zipfullpath = Dts.Variables["User::ZipFullPath"].Value.ToString();
string inputfolder = Dts.Variables["$Project::InputFolder"].Value.ToString();
using (ZipArchive arch = ZipFile.OpenRead(zipfullpath))
{
foreach (ZipArchiveEntry entry in arch.Entries)
{
entry.ExtractToFile(Path.Combine(inputfolder, entry.FullName),true);
//ZipFile.ExtractToDirectory(zipfullpath, inputfolder);
}
}
//File.Delete(zipfullpath);
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
更新
我在代码中做了一些更改,现在我可以创建主/根文件夹,即20160423,但我无法创建子目录(插入,删除,更新)来提取每个文件夹中的.dat文件。当我试图运行代码时,调试提示错误,该路径不存在,因为文件夹插入,更新和删除。
public void Main()
{
// TODO: Add your code here
string zipfullpath = Dts.Variables["User::ZipFullPath"].Value.ToString();
string inputfolder = Dts.Variables["$Project::InputFolder"].Value.ToString();
string rootfolder = Path.GetFileNameWithoutExtension(zipfullpath);
using (ZipArchive arch = ZipFile.OpenRead(zipfullpath))
{
//var root = arch.Entries[0].FullName;
//var result = from curr in arch.Entries
// where Path.GetDirectoryName(curr.FullName) != root
// where !string.IsNullOrEmpty(curr.Name)
// select curr;
foreach (ZipArchiveEntry entry in arch.Entries)
{
//entry.ExtractToFile(Path.Combine(inputfolder, entry.FullName),true);
//ZipFile.ExtractToDirectory(zipfullpath, inputfolder);
//Gets the complete path for the destination file, including any
//relative paths that were in the zip file
string destinationFileName = Path.Combine(inputfolder,rootfolder, entry.FullName);
//Gets just the new path, minus the file name so we can create the
//directory if it does not exist
string destinationFilePath = Path.GetDirectoryName(destinationFileName);
//var newName = entry.FullName.Substring(root.Length);
//Creates the directory (if it doesn't exist) for the new path
Directory.CreateDirectory(destinationFileName);
//if (!File.Exists(destinationFileName) || File.GetLastWriteTime(destinationFileName) < entry.LastWriteTime)
//{
//Either the file didn't exist or this file is newer, so
//we will extract it and overwrite any existing file
entry.ExtractToFile(destinationFileName, true);
//}
}
}
//File.Delete(zipfullpath);
Dts.TaskResult = (int)ScriptResults.Success;
}
答案 0 :(得分:0)