繁体   English   中英

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

[英]How to zip multiple files with similar filenames in 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];
}

我打赌这是这条线:

zip.AddFile(myOtherList[x]); 

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

如果您可以使用Linq(即Framework 3.5或更高版本),您可以执行以下操作

这使用Linq按fileName对文件进行分组(没有扩展名)。 一旦采用这种格式,就可以更容易地迭代它们。

string InputDir="C:\\Temp\\";
string OutputDir="C:\\Temp\\zipped\\";

Directory.SetCurrentDirectory(InputDir);//Will change this later
DirectoryInfo di = new DirectoryInfo(InputDir);
var filenames = di.GetFiles().ToList();

var zipFiles = 
    (
    from r in filenames 
    group r by System.IO.Path.GetFileNameWithoutExtension(r.Name) into results
    select new { results.Key , Filenames = (from r in results select r.FullName) } 
    );


foreach(var r in zipFiles)
{
    string zipFileName = System.IO.Path.Combine(OutputDir , r.Key + ".zip");
    Console.WriteLine("Creating Zip file " + zipFileName);

    ZipFile zip = new ZipFile();

    foreach(var filename in r.Filenames)
    {
        Console.WriteLine("     Adding File " + filename);
        zip.AddFile(filename);
    }
    zip.Save(zipFileName);
}

没有测试过,我也不知道Ionic如何处理zip文件已经存在的情况。

输出目录是否存在? 如果我没记错的话,找不到目录也会抛出FileNotFoundException并且创建文件不会自动创建目录路径,需要事先创建它。

似乎Ionic.Zip.dll库无法正确处理自身。 不确定我是否使用了合适的术语。 在尝试了这么多其他想法后,我想自己如何以不同的方式创建唯一列表。 我的解决方案是不使用LINQ因为我找不到SharpDevelop,而是使用一些旧代码:

//Create a new list to store filenames (minus extension)   
List<String> myOtherList =  new List<String>();

foreach (String strCol in listBox1.Items) 
    myOtherList.Add(strCol.Split('.')[0]);

//Create a new list to store UNIQUE filenames (minus extension)
List<string> uniques = new List<string>();

foreach (string item in myOtherList)
    if (!uniques.Contains(item)) uniques.Add(item);

//Set some static values for the test     
string InputDir="C:\\Temp\\";
string OutputDir="C:\\Temp\\zipped\\";

Directory.SetCurrentDirectory(InputDir);

//Create a new object instances for each new zip file
for (int x=0; x < uniques.Count;x++)
{
    ZipFile zip = new ZipFile();

    zip.AddSelectedFiles(uniques[x]+".*");
    zip.Save(OutputDir+uniques[x]+".zip");//uses OutputDir+
    zip.RemoveSelectedEntries("name = *.*");
}    

暂无
暂无

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

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