C# 使用Ionia.zip DLL使用名称相同但扩展名不同的zip文件

C# 使用Ionia.zip DLL使用名称相同但扩展名不同的zip文件,c#,zipfile,dotnetzip,compact-framework2.0,C#,Zipfile,Dotnetzip,Compact Framework2.0,我需要帮助编写一个函数,该函数可以压缩文件夹中具有相同名称但不同扩展名的所有文件。我正在使用Ionic.Zip dll来实现这一点。我正在使用.Net compact framework 2.0,VS2005。我的代码如下所示: public void zipFiles() { string path = "somepath"; string[] fileNames = Directory.GetFiles(path);

我需要帮助编写一个函数,该函数可以压缩文件夹中具有相同名称但不同扩展名的所有文件。我正在使用Ionic.Zip dll来实现这一点。我正在使用.Net compact framework 2.0,VS2005。我的代码如下所示:

   public void zipFiles()
   {
                string path = "somepath";
                string[] fileNames = Directory.GetFiles(path);
                Array.Sort(fileNames);//sort the filename in ascending order
                string lastFileName = string.Empty;
                string zipFileName = null;
                using (ZipFile zip = new ZipFile())
                    {
                        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                        zip.AddDirectoryByName("Files");

                        for (int i = 0; i < fileNames.Length; i++)
                        {
                            string baseFileName = fileNames[i];
                            if (baseFileName != lastFileName)
                            {
                                zipFileName=String.Format("Zip_{0}.zip",DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
                                zip.AddFile(baseFileName, "Files");
                                lastFileName = baseFileName;
                            }
                        }
                        zip.Save(zipFileName);
                    }
    }
public void zipFiles()
{
string path=“somepath”;
string[]fileNames=Directory.GetFiles(路径);
Array.Sort(文件名);//按升序对文件名进行排序
string lastFileName=string.Empty;
字符串zipFileName=null;
使用(ZipFile zip=new ZipFile())
{
zip.AlternateenCodinguage=ZipOption.as必要时;
zip.AddDirectoryByName(“文件”);
for(int i=0;i
问题:文件夹将有3个同名文件,但其扩展名将不同。现在,这些文件由设备FTP,因此文件名由设备自动生成,我无法控制。因此,例如,文件夹中有6个文件:“ABC123.DON”、“ABC123.TGZ”、“ABC123.TSY”、“XYZ456.DON”、“XYZ456.TGZ”,“XYZ456.TSY”。我必须将名为“ABC123”的3个文件和名为“XYZ456”的其他3个文件压缩在一起。正如我所说,我不知道这些文件的名称,我的函数必须在后台运行。我当前的代码将所有文件压缩在一个压缩文件夹中。
有人能帮我吗?

试试下面的代码

 string path = @"d:\test";

 //First find all the unique file name i.e. ABC123 & XYZ456 as per your example                
 List<string> uniqueFiles=new List<string>();
 foreach (string file in Directory.GetFiles(path))
 {
     if (!uniqueFiles.Contains(Path.GetFileNameWithoutExtension(file)))
         uniqueFiles.Add(Path.GetFileNameWithoutExtension(file));
 }

 foreach (string file in uniqueFiles)
 {
      string[] filesToBeZipped = Directory.GetFiles(@"d:\test",string.Format("{0}.*",file));

      //Zip all the files in filesToBeZipped 
 }
stringpath=@“d:\test”;
//首先根据您的示例查找所有唯一的文件名,即ABC123和XYZ456
List uniqueFiles=新列表();
foreach(Directory.GetFiles(path)中的字符串文件)
{
如果(!uniqueFiles.Contains(Path.GetFileNameWithoutExtension(file)))
Add(Path.GetFileNameWithoutExtension(file));
}
foreach(uniqueFiles中的字符串文件)
{
string[]filesToBeZipped=Directory.GetFiles(@“d:\test”,string.Format(“{0}.*”,file));
//压缩文件中的所有文件以进行压缩
}

你能澄清你的问题细节吗?@lexeRoy:长话短说:使用Ionic.zip压缩所有同名文件。你是用Ionic.zip引用的,对吗?那么它有什么问题?