- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近遇到了一些问题。我一直在尝试将一个 zip 文件提取到内存流中,然后从该流中使用 updateEntry()
方法将其添加到目标 zip 文件中。
问题是,当流中的文件被放入目标 zip 时,如果文件不在 zip 中,它就会工作。如果存在同名文件,则不会正确覆盖。它在 dotnetzip 文档上说,此方法将覆盖 zip 中存在的同名文件,但它似乎不起作用。它会正确写入,但是当我去检查 zip 时,应该被覆盖的文件的压缩字节大小为 0,这意味着出现了问题。
我在下面附上我的代码,向您展示我在做什么:
ZipFile zipnew = new ZipFile(forgeFile);
ZipFile zipold = new ZipFile(zFile);
using(zipnew) {
foreach(ZipEntry zenew in zipnew) {
percent = (current / zipnew.Count) * 100;
string flna = zenew.FileName;
var fstream = new MemoryStream();
zenew.Extract(fstream);
fstream.Seek(0, SeekOrigin.Begin);
using(zipold) {
var zn = zipold.UpdateEntry(flna, fstream);
zipold.Save();
fstream.Dispose();
}
current++;
}
zipnew.Dispose();
}
最佳答案
虽然可能有点慢,但我找到了解决办法,手动删除并添加文件。我会把代码留在这里,以防其他人遇到这个问题。
ZipFile zipnew = new ZipFile(forgeFile);
ZipFile zipold = new ZipFile(zFile);
using(zipnew) {
// Loop through each entry in the zip file
foreach(ZipEntry zenew in zipnew) {
string flna = zenew.FileName;
// Create a new memory stream for extracted files
var ms = new MemoryStream();
// Extract entry into the memory stream
zenew.Extract(ms);
ms.Seek(0, SeekOrigin.Begin); // Rewind the memory stream
using(zipold) {
// Remove existing entry first
try {
zipold.RemoveEntry(flna);
zipold.Save();
}
catch (System.Exception ex) {} // Ignore if there is nothing found
// Add in the new entry
var zn = zipold.AddEntry(flna, ms);
zipold.Save(); // Save the zip file with the newly added file
ms.Dispose(); // Dispose of the stream so resources are released
}
}
zipnew.Dispose(); // Close the zip file
}
关于c# - 将 updateEntry() 方法与 dotnetzip 一起使用不会正确覆盖文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355389/
由于我对 Visual Studio 很陌生,所以这个问题可能听起来有点愚蠢: 如何安装 DotNetZip 库?我正在使用适用于 Windows 桌面的 Visual Studio Express
我有一个非常简单的程序,使用 DotNetZip(最新版本)和 C#.Net、VS2010。当它尝试保存已压缩的文件时,它已开始挂起。没有错误消息,什么也没有。它运行了很长一段时间,然后突然开始出现这
我使用 DotNetZip 库进行了快速测试,该库打开一个充满 .bmp 文件的 zip 文件并将它们转换为 .jpg 格式。 在此之前,我将所有文件写入一个文件夹,转换它们,保存 jpg 文件,然后
我正在使用 DotNetZip从 zip 文件中提取文件的库。 using(ZipFile zip = ZipFile.Read(zipLocation)) { foreach (ZipEnt
嗯! using (ZipFile zip = new ZipFile()) { // add this map file into the "images" directory in
我正在尝试压缩一堆文件并通过流使数据可用。 我希望内存占用尽可能小。 我的想法是实现一个 Stream,其中我有一堆 FileStream 对象作为数据成员。当我的 Stream 上的 Read 方法
我正在使用 C# 进行开发,并使用 Dotnetzip 的源代码在一个文件中获取应用程序。 但是在运行时,我得到了异常: System.OverflowException: Arithmetic op
我正在尝试创建一个将流式传输给用户的 DotNetZip zip 文件。在 zip 文件中,我插入了两个内存流。但是由于某种原因,当我打开 zip 文件时,它是空的。我查看了文档,发现几乎没有帮助。我
我正在使用 DotNetZip。当我正常归档具有英文名称的文件时。但是当我在结果存档中使用错误的文件名存档带有俄罗斯名称的文件时。有人说那个字符串 ZipConstants.DefaultCodePa
这个问题解决了。我的错误是在我的第二个代码块中,我使用的是 SaveProgress 而不是 ExtractProgress,并且在设置事件处理程序之前我使用的是 Zip.ExtractAll。感谢B
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); public
类似的问题都不是我要找的! 下面的代码有什么问题?files是文件内容的文本数组,fileNames是对应的文件名数组。 这段代码总是在倒数第二行的 Save 方法失败,但我不明白为什么流会被关闭!
我已经下载了 DotNetZip from codeplex我完全不知道下一步该怎么做。 我想提取一个.zip 存档 我知道我用的是这样的 string zipToUnpack = "C1P3SML.
当我使用 DotNetZip 创建存档时,它会将文件名中的国家字母更改为其对应的英文字母。 (Ł --> L) 这对我来说真的很糟糕,因为它破坏了我的备份应用程序。我尝试将编码更改为utf8、1252
我有一个在任务中执行压缩解压缩的方法。现在我想要取消操作的能力。但是当我调用 Cancel() 方法时,一切似乎都立即停止并且 while 永远运行: public class OsiSourceZi
我通过以下方式使用 dotnet zip: using (ZipFile zip = new ZipFile(@"E:\test2.zip")) { zip.ParallelDeflate
我正在尝试使用 DotNetZip 库将文件提取到当前工作目录的方法,虽然我似乎无法做到这一点,但它需要一个文件路径: private void unzipfiles() { using (v
问候.... 我正在用 c# 3.5 编写一个备份程序,使用的是最新的 DotNetZip。该程序的基础是在服务器上指定一个位置和跨区 zip 文件的最大大小,然后运行。从那里它应该遍历给定位置的所有
我无法想象这很难做到,但我一直无法让它发挥作用。我有一个文件类,它只存储我要压缩的文件的位置、目录和名称。我正在压缩的文件存在于磁盘上,因此 FileLocation 是完整路径。磁盘上不存在 Zip
我正在使用 DotNetZip 将文件从 MemoryStream 添加到 zip 文件,然后将该 zip 保存为 MemoryStream,以便我可以将其作为附件通过电子邮件发送.下面的代码没有错误
我是一名优秀的程序员,十分优秀!