繁体   English   中英

批量C#中的Zip文件

[英]Zip files in C# in batches

我正在使用Zip存档方法来压缩文件夹中的文件。 但是,需要在特定时间从一个文件夹中读取最多10个文件并将其压缩。 如果有大量文件(例如100),则需要使用C#创建10个zip文件夹。

我该如何实现? 我已经在Windows表单中尝试过-

private void btnZip_Click(object sender, EventArgs e) {  

    string FolderPathToZip = txtFolderPath.Text.Trim();  
    //To create unique file name with date and time with nanoseconds.  
    string ZipFileName = "D:\\backup\\bak-" + DateTime.Now.ToString("ddMMyyyy-HHmmssfffff") + ".zip";  
    try {  
        //To check whether D:\Backup folder exists or not.  
        //If not exists this will create a BACKUP folder.  
        if (Directory.Exists("D:\\backup")) {} else {  
            Directory.CreateDirectory("D:\\backup");  
        }  
        //TO create a zip file.  
        ZipFile.CreateFromDirectory(FolderPathToZip, ZipFileName);  
    } catch (Exception) {  
        //If system throw any exception message box will display "SOME ERROR"  
        MessageBox.Show("Some Error");  
    }  
    //Display successfully created message to the user.  
    MessageBox.Show("Zip Filename : " + ZipFileName + " Created Successfully");  
} 

伪代码:

  1. 为文件所在的路径创建DirectoryInfo对象
  2. 使用DirectoryInfo.GetFiles()获取所有文件的列表(*当前)
  3. 在循环中(i = 0; 10 * i <fileList.Count; i ++)执行以下操作

    • 子集= fileList.Skip(10 * i).Take(10);
    • 遍历此子集并创建Zip存档

要开心 :)

暂无
暂无

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

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