[英]C# SharpCompress Append to Existing Archive
我正在使用SharpCompress( https://sharpcompress.codeplex.com ),但是我很难附加到ZIP文件。
我已经在Stream上尝试过FileMode.Append,但是它仍然会覆盖ZIP,并且经过处理后,ZIP文件中只有一个文件。
我无法一次对所有文件使用该过程,因为在任何时间点并非所有文件都可用
WriterOptions writerOptions = new WriterOptions(CompressionType.LZMA);
using (var zip = File.Open(strLogPath + "\\" + ArchiveName(strFile, strArchiveInterval), FileMode.Append))
//using (var zip = File.OpenWrite(strLogPath + "\\" + ArchiveName(strFile, strArchiveInterval)))
using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, writerOptions))
{
zipWriter.Write(Path.GetFileName(strFile), strFile);
}
SharpCompress测试代码包括以下示例:
[Fact]
public void Zip_Random_Write_Add()
{
string jpg = Path.Combine(ORIGINAL_FILES_PATH, "jpg\\test.jpg");
string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.mod.zip");
string unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip");
string modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod2.zip");
base.ResetScratch();
using (var archive = ZipArchive.Open(unmodified))
{
archive.AddEntry("jpg\\test.jpg", jpg);
archive.SaveTo(scratchPath, CompressionType.Deflate);
}
CompareArchivesByPath(modified, scratchPath);
}
因此,大概在您的情况下,您需要执行以下操作:
string zipPath = Path.Combine(strLogPath, ArchiveName(strFile, strArchiveInterval));
string entryPath = @"where\you\want\it\in\the\zip";
string filePath = @"C:\temp\newfiletoadd";
using (var archive = ZipArchive.Open(zipPath)))
{
archive.AddEntry(entryPath, filePath);
archive.SaveTo(zipPath, CompressionType.LZMA);
}
如果它抱怨它已经打开以供读取而不能覆盖文件(因为我没有测试过),则您可能需要一些额外的步骤将新档案保存到临时文件(或存储流中,如果很小的话) ,然后删除原始文件,然后将临时文件移动到原始位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.