如何在 C# 中压缩具有相似文件名的多个文件
有人对如何获取文件夹并压缩每个类似名称的组有任何建议吗?这 将是相同的名称而不是扩展名。
这是我整理的代码草稿:
//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];
}
Would anyone have any suggestions on how to take a folder and zip each group of similar name. This
would be names which are the same not the extension.
Here is a rough draft of code I put together:
//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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我打赌是这一行:
此时,它必须找到有问题的 zip 文件。事实上,您重复使用完全相同的对象,这使得调试变得更加困难,因此我会考虑进入其自己的例程,并将单个 zip 文件中的 zip 名称和 zip 文件路径提供给例程。然后,您创建一个新的 zip 并添加文件,而不是重复使用相同的 zip 对象。
I am betting it is this line:
At this point, it has to find the zip file in question. The fact you are reusing the exact same object makes this harder to debug, so I would consider moving into its own routine and feeding the name of the zip and the file paths to zip in a single zip file to the routine. You then create a new zip and add files rather than reuse the same zip object.
如果您可以使用 Linq(即 Framework 3.5 或更高版本),您可以执行类似以下操作。
这使用 Linq 按文件名(不带扩展名)对文件进行分组。一旦采用这种格式,就可以更轻松地迭代它们。
还没有测试过这个,而且我也不知道 Ionic 如何处理 zip 文件已经存在的情况。
If you can use Linq (ie Framework 3.5 or later), you could do something like the following
This uses Linq to group the files by the fileName (without the extension). Once in this format, it makes it easier to iterate through them.
Haven't tested this, and also I don't know how Ionic handles the case where the zip file already exists.
输出目录是否存在?如果我没记错的话,找不到目录也会抛出
FileNotFoundException
并且创建文件不会自动创建目录路径,需要事先创建。Does the output directory exist? If I remember correctly, not finding the directory also throws a
FileNotFoundException
and creating a file will not automatically create the directory path, it needs to be created before hand.似乎 Ionic.Zip.dll 库无法正确处理自身。不确定我使用的术语是否正确。在尝试了很多其他想法之后,我想如果我以不同的方式创建独特的列表怎么样。瞧,我的解决方案没有使用 LINQ,因为我找不到 SharpDevelop,而是使用一些旧代码:
Seems that the Ionic.Zip.dll library can't dispose of itself correctly. Not sure I'm using the right term. After trying so many other ideas I think to myself how about if I create the unique list differently. Voila my solution is not using LINQ cause I can't find it for SharpDevelop but instead some old code: