【问题标题】:Generate multiple smaller zip files from Folder c#从文件夹 c# 生成多个较小的 zip 文件
【发布时间】:2020-08-14 15:41:39
【问题描述】:

我有一个文件夹,其中包含我必须通过慢速网络复制的文件。

我按如下方式生成 zip。

    string path = @".\Files";
    string zipPath = @".\transfer.zip";

    ZipFile.CreateFromDirectory(path , zipPath);

生成的 zip 文件为 10GB,复制单个文件很慢。我想为最大 1GB 的文件夹生成 zip 文件,所以在这种情况下它将生成 10 个。

有没有办法在 C# 中做到这一点

【问题讨论】:

  • “有没有办法在 C# 中做到这一点” -> 是的,有。您必须一次将一个文件添加到 zip 文件中,而不是使用方便的方法 CreateFromDirectory
  • 所以没有调用按文件大小对 zip 创建进行分块?
  • 我不这么认为。

标签: c# .net zip zipfile


【解决方案1】:

我只是把它放在一起并测试了它。这有点粗糙,但我只是暂时下班休息。请注意,它跟踪原始文件大小而不是压缩文件大小。因此,它会占用多达 1GB 的原始文件并对其进行转换,然后继续处理其余文件。

        //maximum size in bytes (1gb)
        long maxSize = 1073741824;                                      
        string path = @".\Files";
        string zipPath = @".\transfer.zip";
        //keep track of sum of sizes of raw files to be zipped.
        long currentSize = 0;
        //get list of files to iterate through.
        string[] filesToCompress = Directory.GetFiles(path, "*.*");
        //declare file counter so you don't duplicate filenames.
        int fileCounter = 1;
        //create empty list to store file paths to be zipped.
        List<string> zipMe = new List<string>();
        string zipName = string.Empty;
        foreach (string file in filesToCompress)
        {
            zipName = "myfile_" + fileCounter + ".zip";
            FileInfo f = new FileInfo(file);
            if ((f.Length + currentSize) >= maxSize)
            {
                //create zip file
                using (ZipArchive newFile = ZipFile.Open(zipPath+zipName, ZipArchiveMode.Create))
                {
                    foreach (string item in zipMe)
                    {
                        newFile.CreateEntryFromFile(item, item.Replace(path + "\\", ""));
                    }
                }
                //clear the list
                zipMe.Clear();
                //add the file that would have put it over the file size limit to the beginning of the next list.
                zipMe.Add(f.FullName);
                //increase counter for filename.
                fileCounter++;
                //set currentsize equal to the file's size we just added to the list.
                currentSize = f.Length;
            }
            else
            {
                //Haven't hit 1gb yet...add file's length to current length as well as add it to the list and keep on going!
                currentSize = currentSize + f.Length;
                zipMe.Add(f.FullName);
            }               
        }
        //Loop is done, let's clear up any files left over in the list that didne't exceed our file size limit.
        if (zipMe.Count > 0)
        {
            using (ZipArchive newFile = ZipFile.Open(zipPath + zipName, ZipArchiveMode.Create))
            {
                foreach (string item in zipMe)
                {
                    newFile.CreateEntryFromFile(item, item.Replace(path + "\\", ""));
                }
            }
        }

【讨论】:

    【解决方案2】:

    【讨论】:

    • 这篇博文很旧,您发送的网址中的链接已失效。谢谢,我会研究包装thugh
    猜你喜欢
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多