Skip to main content
 首页 » 编程设计

c#之如何在 C# 中压缩具有相似文件名的多个文件

2024年02月23日19三少

有没有人对如何制作一个文件夹并压缩每组相似名称的人有任何建议。这 将是与扩展名相同的名称。

这是我整理的代码草稿:

//When it gets to the 3rd file it for some reason throws an exception 
//System.IO.FileNotFoundException: Could not find file 'c:\Temp\zipped\fileb.zip'. 
//The funny thing is that I would figure it is trying to write this new filename why look for it 
//This uses the Ionic.Zip.dll library  
string InputDir="C:\\Temp\\"; 
string OutputDir="C:\\Temp\\zipped\\"; 
string prevFilename=""; 
ZipFile zip = new ZipFile(); 
 
Directory.SetCurrentDirectory(InputDir);//Will change this later 
for (int x=0; x < myOtherList.Count;x++) 
{ 
    string fullfilename = myOtherList[x]; 
    string[] fileDOTextension = fullfilename.Split('.'); 
 
    if ((fileDOTextension[0]!=prevFilename)&&(x!=0)) 
    { 
        zip.Save(OutputDir+prevFilename+".zip"); 
        zip.RemoveSelectedEntries("name = *.*"); 
    } 
 
    zip.AddFile(myOtherList[x]); 
    prevFilename=fileDOTextension[0]; 
} 

请您参考如下方法:

我打赌是这条线:

zip.AddFile(myOtherList[x]);  

此时,它必须找到有问题的 zip 文件。事实上,您正在重复使用完全相同的对象,这使得调试变得更加困难,因此我会考虑进入它自己的例程,并将 zip 的名称和要在单个 zip 文件中压缩的文件路径提供给例程。然后您创建一个新的 zip 并添加文件,而不是重复使用相同的 zip 对象。