简体   繁体   English

避免使用Xceed.Zip.QuickZip.Zip实用程序创建不必要的目录

[英]Avoid creating unnecessary directories using Xceed.Zip.QuickZip.Zip utility

I am using the paid version of Xceed.Zip.QuickZip.Zip utility in my application to compress a folder that contains files and directories. 我在我的应用程序中使用Xceed.Zip.QuickZip.Zip实用程序的付费版本来压缩包含文件和目录的文件夹。 Here is the Xceed Zip command that I am using 这是我正在使用的Xceed Zip命令

Xceed.Zip.Licenser.LicenseKey = XceedZipKey;
Xceed.Zip.QuickZip.Zip(ZipFilePath, true, true, true, filesToZip);

ZipFilePath is the name of thr final zip including the path where the zip will be created. ZipFilePath是最终zip的名称,包括将在其中创建zip的路径。 So it is like D:\\MyData/MyZip.zip . 就像D:\\MyData/MyZip.zip

And, filesToZip is like this D:\\MyDataToZip\\Versions\\\\*.* . 而且,filesToZip就像这样D:\\MyDataToZip\\Versions\\\\*.*

So when the zip is created, I see a MyDataToZip folder, which contains Versions folder and then everything that is inside the Version folder. 因此,在创建zip时,我看到一个MyDataToZip文件夹,其中包含Versions文件夹,然后包含Version文件夹中的所有内容。

But I want the zip to not contain the MyDataToZip & Versions folder. 但是我希望压缩文件不包含MyDataToZip&Versions文件夹。 It should start from Versions folder. 它应该从Versions文件夹开始。 How can I achieve that? 我该如何实现?

Please note that my application is built on .Net 4.0, so I can't use System.IO.Compression. 请注意,我的应用程序基于.Net 4.0构建,因此不能使用System.IO.Compression。

Use the Xceed FileSystem API in order to preserve the file paths from the target folder down. 使用Xceed FileSystem API可以保留目标文件夹下方的文件路径。

public void ZipFolder(string zipFilePath, string folderPath, bool recursive, bool replaceExistingFiles, params object[] filters)
{
    AbstractFile zipFile = new DiskFile(zipFilePath);
    ZipArchive zip = new ZipArchive(zipFile);

    using (AutoBatchUpdate batch = new AutoBatchUpdate(zip))
    {
        AbstractFolder sourceFolder = new DiskFolder(folderPath);
        sourceFolder.CopyFilesTo(zip, recursive, replaceExistingFiles, filters);
    }
}

you need to use 你需要使用

Xceed.Zip.QuickZip.Zip(ZipFilePath, true, true, false, filesToZip);

as the 4th parameter is: 因为第四个参数是:

preservePaths Boolean value indicating if the directory structure should be preserved in the zip file. preservePaths布尔值,指示是否应在zip文件中保留目录结构。

REF: Zip(String,Boolean,Boolean,Boolean,String[]) Method REF:Zip(String,Boolean,Boolean,Boolean,String [])方法

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

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