Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中压缩具有相似文件名的多个文件#_C#_File_Process_Zip - Fatal编程技术网

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

C# 如何在C中压缩具有相似文件名的多个文件#,c#,file,process,zip,C#,File,Process,Zip,有没有人对如何获取一个文件夹并压缩每个名称相似的组有什么建议。这 将是相同的名称,而不是扩展名 下面是我编写的代码草稿: //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 fi

有没有人对如何获取一个文件夹并压缩每个名称相似的组有什么建议。这 将是相同的名称,而不是扩展名

下面是我编写的代码草稿:

//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];
}
//当它到达第三个文件时,出于某种原因会抛出一个异常
//System.IO.FileNotFoundException:找不到文件“c:\Temp\zip\fileb.zip”。
//有趣的是,我想它是在试图写这个新的文件名,为什么要找它呢
//这将使用Ionic.Zip.dll库
字符串InputDir=“C:\\Temp\\”;
字符串OutputDir=“C:\\Temp\\zipped\\”;
字符串prevFilename=“”;
ZipFile zip=新ZipFile();
目录.SetCurrentDirectory(InputDir)//稍后将更改此设置
对于(int x=0;x
我打赌是这一行:

zip.AddFile(myOtherList[x]); 

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

我打赌这是一行:

zip.AddFile(myOtherList[x]); 
此时,它必须找到有问题的zip文件。事实上,您正在重用完全相同的对象,这使得调试更加困难,所以我会考虑进入自己的例程,并将ZIP的名称和文件路径以zip文件压缩到例程。然后创建一个新的zip并添加文件,而不是重复使用同一个zip对象。

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

这将使用Linq按文件名(不带扩展名)对文件进行分组。一旦采用这种格式,就可以更容易地遍历它们

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);
}
我还没有测试过这个,而且我也不知道爱奥尼亚如何处理zip文件已经存在的情况

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

这将使用Linq按文件名(不带扩展名)对文件进行分组。一旦采用这种格式,就可以更容易地遍历它们

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

我还没有测试过这个,而且我也不知道爱奥尼亚如何处理zip文件已经存在的情况

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

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

似乎Ionic.Zip.dll库无法正确处理自身。我不确定我用的是正确的术语。在尝试了这么多其他的想法之后,我想,如果我以不同的方式创建独特的列表,会怎么样。瞧,我的解决方案没有使用LINQ,因为我找不到SharpDevelop的LINQ,而是一些旧代码:

//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 = *.*");
}    
//创建一个新列表来存储文件名(减去扩展名)
List myOtherList=新列表();
foreach(listBox1.Items中的字符串strCol)
myOtherList.Add(strCol.Split('.')[0]);
//创建新列表以存储唯一的文件名(减去扩展名)
List uniques=新列表();
foreach(myOtherList中的字符串项)
如果(!uniques.Contains(item))uniques.Add(item);
//为测试设置一些静态值
字符串InputDir=“C:\\Temp\\”;
字符串OutputDir=“C:\\Temp\\zipped\\”;
目录.SetCurrentDirectory(InputDir);
//为每个新zip文件创建一个新的对象实例
对于(int x=0;x
似乎Ionic.Zip.dll库无法正确处理自身。我不确定我用的是正确的术语。在尝试了这么多其他的想法之后,我想,如果我以不同的方式创建独特的列表,会怎么样。瞧,我的解决方案没有使用LINQ,因为我找不到SharpDevelop的LINQ,而是一些旧代码:

//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 = *.*");
}    
//创建一个新列表来存储文件名(减去扩展名)
List myOtherList=新列表();
foreach(listBox1.Items中的字符串strCol)
myOtherList.Add(strCol.Split('.')[0]);
//创建新列表以存储唯一的文件名(减去扩展名)
List uniques=新列表();
foreach(myOtherList中的字符串项)
如果(!uniques.Contains(item))uniques.Add(item);
//为测试设置一些静态值
字符串InputDir=“C:\\Temp\\”;
字符串OutputDir=“C:\\Temp\\zipped\\”;
目录.SetCurrentDirectory(InputDir);
//为每个新zip文件创建一个新的对象实例
对于(int x=0;x
FileNotFoundException是FileNotFoundException。重新检查异常从何处生成。使用调试器提供帮助。您的拆分可能无法正常工作。如果文件名中有句点,例如system.data.dll,该怎么办?要获取不带扩展名的文件名,应该使用Path.GetFilenameWithoutExtension(filename),它在以下行引发异常:-->zip.Save(OutputDir+prevFilename+“.zip”),这没有意义,因为它为什么要查找它试图创建的文件。它应该只查找添加到队列中的项目。只要文件名被清晰地分割。嗯,到目前为止我已经