存档 zip 文件并将其保存到所选位置,无需额外的文件路径



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

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

该文件保存到我的路径中,但是当我打开它时,它有文件夹用户,然后在那里它有文件夹 Shaun,在该文件夹中有一个文件夹文档,在文件夹 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>
List<string> filesToBeAdded = new List<string>();
filesToBeAdded.Add("C://Users//Shaun//Documents//FormValue//" + property_details_locality_map);
filesToBeAdded.Add("C://Users//Shaun//Documents//FormValue//data.xml");
filesToBeAdded.Add("C://Users//Shaun//Documents//FormValue//dvform.dvform");
zip.AddFiles(filesToBeAdded, false, "ShaunXAP"); // you could pass empty string here instead of "ShaunXAP" 
zip.Save("C://Users//Shaun//Documents//FormValue//NewValuation.xap");

这会将所有文件放入一个公共文件夹(在本例中为"ShaunXAP")中,并忽略已存档文件的文件夹层次结构。

最新更新