C# 使用 ionic.zip 创建一个 zip 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15291337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a zip file with ionic.zip
提问by Richard.Gale
I have the following code set up to create a zip file of a set of doucments:
我设置了以下代码来创建一组文档的 zip 文件:
public bool CreateDocumentationZipFile(int documentIdentifier, string zipDestinationPath, IList<string> documentPaths)
{
bool zipped = false;
if (documentPaths.Count > 0)
{
using (ZipFile loanZip = new ZipFile())
{
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
loanZip.Save(string.Format("{0}{1}.zip",zipDestinationPath, documentIdentifier.ToString()));
zipped = true;
}
}
return zipped;
}
The issue I have is that when the zip file is created, the folder structure is maintaned within the zip file:
我遇到的问题是,当创建 zip 文件时,文件夹结构会在 zip 文件中维护:
e.g
例如
I am creating a zip of a selection of documents located at
我正在创建位于以下位置的精选文档的 zip
C:\SoftwareDevelopment\Branches\ScannedDocuments\
C:\SoftwareDevelopment\Branches\ScannedDocuments\
When the created zip file is opened, there is a folder structure within the zip as follows:
当创建的 zip 文件打开时,zip 中有一个文件夹结构,如下所示:
Folder 1 ("SoftwareDevelopment")
文件夹 1(“软件开发”)
Inside Folder 1 is folder 2 ("Branches")
文件夹 1 内是文件夹 2(“分支”)
Inside Folder 2 is folder 3 ("ScannedDocuments")
文件夹 2 内是文件夹 3(“ScannedDocuments”)
the scanned documents folder then contains the actual scan files.
然后扫描的文档文件夹包含实际的扫描文件。
Can anyone tell me how I can just have the scan files in the zip without the folders path being maintained?
谁能告诉我如何在不维护文件夹路径的情况下将扫描文件放在 zip 中?
采纳答案by Treb
The documentationstates that the third parameter
该文件指出,第三个参数
directoryPathInArchive (String)
Specifies a directory path to use to override any path in the file name. This path may, or may not, correspond to a real directory in the current filesystem. If the files within the zip are later extracted, this is the path used for the extracted file. Passing null (Nothing in VB) will use the path on each of the fileNames, if any. Passing the empty string ("") will insert the item at the root path within the archive.
directoryPathInArchive(字符串)
指定用于覆盖文件名中的任何路径的目录路径。该路径可能对应也可能不对应于当前文件系统中的真实目录。如果 zip 中的文件稍后被解压,则这是用于解压文件的路径。传递 null(在 VB 中没有)将使用每个文件名上的路径,如果有的话。传递空字符串 ("") 将在存档内的根路径中插入项目。
So if you always want to have the files added to the root of your zip archive, change
因此,如果您始终希望将文件添加到 zip 存档的根目录,请更改
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
to
到
loanZip.AddFiles(documentPaths, false, "");