从Folder c#生成多个较小的zip文件

问题描述

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

我按如下所示生成zip。

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

    ZipFile.CreateFromDirectory(path,zipPath);

生成的zip文件为10GB,并且复制单个文件的速度很慢。我想为该文件夹生成最大1GB的zip文件,因此在这种情况下它将生成10个。

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

解决方法

我只是把它们放在一起并进行了测试。这有点粗糙,但我只是下班休息一下。请注意,它会跟踪原始文件大小,而不是压缩文件大小。因此,它将占用多达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,""));
                }
            }
        }
,

您可以查看以下内容:https://docs.microsoft.com/en-us/dotnet/api/system.io.packaging?redirectedfrom=MSDN&view=dotnet-plat-ext-3.1

它具有支持在单个容器中存储多个objetcs的类。

可以在此处找到另一个示例:https://www.c-sharpcorner.com/blogs/create-a-multi-zip-file-from-a-large-file-and-upload-one-part-at-a-time1

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...