问题描述
我使用压缩包含子目录的文件夹,此操作正常。我想要做的是剥离第一个子文件的父目录,所以不相关的整个结构不执行...
I am using SharpZipLib to zip up a folder with subdirectories and this is working fine. What I would like to do is strip off the parents directories of the first child file so the whole structure that is irrelevant isn't carried forth...
示例:
c:\a\b\c\d\e\f\g\h\file1.txt
c:\a\b\c\d\e\f\g\h\file1.txt
c:\a\b\c\d\e\f\g\h\file2.txt
c:\a\b\c\d\e\f\g\h\file2.txt
c:\a\b\c\d\e\f\g\h\i\file1.txt
c:\a\b\c\d\e\f\g\h\i\file1.txt
c:\a\b\c\d\e\f\g\h\i\file2.txt
c:\a\b\c\d\e\f\g\h\i\file2.txt
它应该是这样的:
file2.txt
file2.txt
i\file1.txt
i\file1.txt
i\file2.txt
i\file2.txt
我如何做到这一点?
/ p>
Here is the code I have so far:
ZipFile zipFile = new ZipFile(destinationArchive);
zipFile.BeginUpdate();
foreach (FileInfo file in sourceFiles)
{
zipFile.Add(file.FullName);
}
zipFile.CommitUpdate();
zipFile.Close();
推荐答案
改用ZipOutputStream:
Use ZipOutputStream instead:
string[] sourceFiles = new [] { @"c:\a\b\c\d\e\f\g\h\file1.txt", @"c:\a\b\c\d\e\f\g\h\i\file1.txt" };
FileStream fileStream = File.Create(@"c:\temp\test.zip");
ZipOutputStream zipOut = new ZipOutputStream(fileStream);
string baseDir = @"c:\a\b\c\d\e\f\g\h\";
foreach (var sourceFile in sourceFiles)
{
ZipEntry entry = new ZipEntry(sourceFile.Replace(baseDir,""));
zipOut.PutNextEntry(entry);
FileStream inFile = File.OpenRead(sourceFile);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0)
{
zipOut.Write(buffer,0,bytesRead);
}
zipOut.CloseEntry();
}
zipOut.Close();
这篇关于C#SharpZipLib剥离不相关的目录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!