简体   繁体   English

如何使用 SevenZipSharp 将文件添加到存档

[英]How to add files to archive using SevenZipSharp

First of all what I want to do:首先我想做的是:

I have a list of files I'd like to add to the same archive.我有一个要添加到同一存档中的文件列表。 The folder structure to this files should be included to the archive.此文件的文件夹结构应包含在存档中。

The problem I have is that I can not add files to an existing archive.我遇到的问题是我无法将文件添加到现有存档中。 When I use CompressionMode.Create only the actual file is in the archive, when I use CompressionMode.Append I get a KeyNotFoundException and nothing changed on the archive.当我使用CompressionMode.Create时,只有实际文件在存档中,当我使用CompressionMode.Append时,我得到一个KeyNotFoundException并且存档上没有任何变化。

SevenZip.SevenZipCompressor szc = new SevenZip.SevenZipCompressor();
if (File.Exists(PathToArchive))
    szc.CompressionMode = SevenZip.CompressionMode.Append;
else
    szc.CompressionMode = SevenZip.CompressionMode.Create;

FileStream archive = new FileStream(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
    szc.DirectoryStructure = true;
    szc.EncryptHeaders = true;
    szc.DefaultItemName = filename; //if the full path given the folders are also created
    szc.CompressStream(filestream, archive, Password);
}
catch (Exception e) { }

archive.Close();

I'm not having any problem to append files to an existing archive, with SharpZipLib 0.64 (from the Nuget) and 7z.dll 9.20 from sourceforge, but I'm using CompressFiles() instead of CompressStream().我对 append 文件到现有存档没有任何问题,使用 SharpZipLib 0.64(来自 Nuget)和来自 sourceforge 的 7z.dll 9.20,但我使用的是 CompressFiles() 而不是 CompressStream()。

This Operation is NOT supported by 7-Zip, even when using its file manager. 7-Zip 不支持此操作,即使使用其文件管理器也是如此。 I suggest you just delete the old archive and recreate it with new files.我建议您只需删除旧存档并使用新文件重新创建它。

Windows 7 x64 - 7-Zip 9.20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM