c# - 归档 zip 文件并将其保存到选择的位置,而不需要额外的文件路径?

标签 c# zip

我正在尝试保存一个 XAP 文件,它基本上就像一个 zip 文件,我可以存档并保存它,但它会添加到许多文件夹?

我正在使用 Ionic.Zip DLL 来存档我的 XAP 文件。

该文件保存到我的路径中,但是当我打开它时,它有文件夹 users,然后在那里有文件夹 Shaun,在该文件夹中有一个文件夹 Documents,在文件夹 FormValue 中,然后在其中有我压缩了 3 个文件。

我只需要 Xap 文件来包含我压缩的 3 个文件,而不需要包含其中的所有额外文件夹。

using (ZipFile zip = new ZipFile())
{
// add this map to zip
zip.AddFile("C://Users//Shaun//Documents//FormValue//" + property_details_locality_map); 
zip.AddFile("C://Users//Shaun//Documents//FormValue//data.xml");
zip.AddFile("C://Users//Shaun//Documents//FormValue//dvform.dvform"); 
zip.Save("C://Users//Shaun//Documents//FormValue//NewValuation.xap");
}

最佳答案

使用 zip.AddFile(string fileName, string directoryPathInArchive) 重载并为第二个参数指定空字符串 "":

zip.AddFile("C://Users//Shaun//Documents//FormValue//" + property_details_locality_map, ""); 
zip.AddFile("C://Users//Shaun//Documents//FormValue//data.xml", "");
zip.AddFile("C://Users//Shaun//Documents//FormValue//dvform.dvform", ""); 

来自文档:

/// <param name="directoryPathInArchive">
///   Specifies a directory path to use to override any path in the fileName.
///   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 <c>null</c> (<c>Nothing</c> in
///   VB) will use the path on the fileName, if any.  Passing the empty string
///   ("") will insert the item at the root path within the archive.
/// </param>

关于c# - 归档 zip 文件并将其保存到选择的位置,而不需要额外的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12670267/

相关文章:

javascript - 如何使用JSZip解压

c# - 无法理解 "A field initializer cannot reference the nonstatic field"错误?

c# - 为什么我不能在 C# 中捕获通用异常?

c# - Controller 中的 HttpPostedFileBase Null - 想要将文件路径保存到数据库

java - 有哪些不同的 zlib 压缩方法以及如何在 Java 的 Deflater 中强制使用默认值?

perl - 如何检测程序是否在 PAR 存档中运行?

php - 如何获取 NSString 中携带的 UTF8 编码版本的数据并将其返回给 NSData?

c# - 如何正确发送PATCH请求

c# - Web 形式的智能卡授权

python - 如何使用两个数组作为返回值压缩函数 - Python