我必须在使用ICSharpCode.SharZipLib.Zip以编程方式创建的zip文件中创建两个文件夹。我想:
private void AddToZipStream(byte[] inputStream, ZipOutputStream zipStream, string fileName, string fileExtension)
{
var courseName = RemoveSpecialCharacters(fileName);
var m_Bytes = inputStream;
if ((m_Bytes != null) && (zipStream != null))
{
var newEntry = new ZipEntry(ZipEntry.CleanName(string.Concat(courseName, fileExtension)));
newEntry.DateTime = DateTime.Now;
newEntry.Size = m_Bytes.Length;
zipStream.PutNextEntry(newEntry);
zipStream.Write(m_Bytes, 0, m_Bytes.Length);
zipStream.CloseEntry();
zipStream.UseZip64 = UseZip64.Off;
}
}如何使用ZipEntry创建目录,然后如何将文件添加到Zip存档中的目录中?
发布于 2013-08-21 18:02:48
我想出来了:
new ZipEntry("Folder1/Archive.txt");和new ZipEntry("Folder2/Archive2.txt");发布于 2016-07-30 08:09:34
上面的答案将适用于几种情况,但当您想要将空文件夹添加到zip文件时,它将无法工作。
我仔细研究了SharpZipLib代码,发现创建文件夹唯一需要做的事情是ZipEntry名称上的尾随"/“斜杠。
下面是库中的代码:
public bool IsDirectory {
get {
int nameLength = name.Length;
bool result =
((nameLength > 0) &&
((name[nameLength - 1] == '/') || (name[nameLength - 1] == '\\'))) ||
HasDosAttributes(16)
;
return result;
}
}因此,只需创建文件夹,就像它们是带有ZipEntry的文件一样,并在末尾放置一个正斜杠。它起作用了。我测试过了。
发布于 2017-09-28 14:52:08
在我们的项目中,最好的解决方案是转向更好的方式。
https://dotnetzip.codeplex.com
https://github.com/haf/DotNetZip.Semverd
这些方法使用起来更直接。
https://stackoverflow.com/questions/18363187
复制相似问题