简体   繁体   English

如何在c#中只压缩路径中的文件和文件夹

[英]How to zip only file and folder in path in c#

I use this code for zip all file and folder in my path.我使用此代码压缩路径中的所有文件和文件夹。

using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(@"MyDocuments\ProjectX", "ProjectX");
zip.Save(zipFileToCreate);
}

For example:例如:

  • Folder1文件夹 1
    • Folder2文件夹 2
      • file1文件 1
      • file2文件 2
      • file3文件 3
    • file4文件 4
    • file5文件5
    • file6文件 6

I zip Folder1, and this code it's working.我压缩了 Folder1,这段代码正在运行。 but this zip all file and folder with Folder1.但这会使用 Folder1 压缩所有文件和文件夹。 but i have zip only file and folder in Folder1.但我在 Folder1 中只有 zip 文件和文件夹。

Result my code:结果我的代码:

  • MyFileZip我的档案

    • Folder1文件夹 1

      • Folder2文件夹 2

        • file1文件 1
        • file2文件 2
        • file3文件 3
      • file4文件 4

      • file5文件5
      • file6文件 6

But I want this result:但我想要这个结果:

  • MyFileZip我的档案

    • Folder2文件夹 2

       - file1 - file2 - file3
    • file4文件 4

    • file5文件5
    • file6文件 6

I wonder how could you manage to instantiate ZipFile class as its static, any way use this code 我不知道您如何设法将ZipFile类实例化为其静态实例,以任何方式使用此代码

    string startPath = @"<path-to-folder1>";
    string zipPath = @"<path-to-output>\MyFileZip.zip";

    ZipFile.CreateFromDirectory(startPath, zipPath);

Just remember that the destination folder can not be same as folder1 or you get an exception claiming process is in use 请记住,目标文件夹不能与folder1相同,否则您将获得异常声明,声称进程正在使用中

You could use the following static method override and specify includeBaseDirectory to be false 您可以使用下面的静态方法重写,并将includeBaseDirectory指定为false

   public static void CreateFromDirectory (string sourceDirectoryName,
    string destinationArchiveFileName,
    System.IO.Compression.CompressionLevel compressionLevel, bool
    includeBaseDirectory, System.Text.Encoding entryNameEncoding);

Documentation 文档

If the destination Folder of the zipped files should be the same as the source Folder, you could use the User Temp directory as transitory storage. 如果压缩文件的目标文件夹应与源文件夹相同,则可以将“用户Temp目录”用作临时存储。
This because you can't create a Zip file in the same directory that contains the files/folders to compress: it'ld try to zip itself, causing an exception. 这是因为您无法在包含要压缩的文件/文件夹的目录中创建一个Zip文件:它会尝试自行压缩,从而导致异常。

string sourceFolder = 
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ProjectX");
string destinationFolder = sourceFolder;
string ZippedFileName = "ZippedFile.zip";

string userTempFolder = Environment.GetEnvironmentVariable("Temp", EnvironmentVariableTarget.User);
string zippedTempFile = Path.Combine(userTempFolder, ZippedFileName);
if (File.Exists(zippedTempFile)) { File.Delete(zippedTempFile); }

ZipFile.CreateFromDirectory(sourceFolder, ZippedFileName);
File.Move(zippedTempFile, Path.Combine(destinationFolder, ZippedFileName));

The last parameter to ZIPFile.CreateFromDirectory, for which you are passing the value true, determines whether the directory itself should be included as the root of the ZIP. ZIPFile.CreateFromDirectory 的最后一个参数(您为其传递值 true)确定目录本身是否应作为 ZIP 的根目录包含在内。 If you change this to false it should work as you desire.如果您将其更改为 false,它应该可以按您的意愿工作。

ZipFile.CreateFromDirectory(startPath, zipPath,  CompressionLevel.Fastest, false); 

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

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