开发者

How to zip multiple files with similar filenames in C#

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];
}


I am betting it is this line:

zip.AddFile(myOtherList[x]); 

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.


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.

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);
}

Haven't tested this, and also I don't know how Ionic handles the case where the zip file already exists.


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.


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:

//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 = *.*");
}    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜