1

如何使用 ICSharplib 压缩文件夹。有什么方法可以在压缩时添加加密密码?没有我可以使用任何其他 dll 的选项。必须只使用 ICSharplib。

目前我正在使用这个代码块

private static void CompressFiles(string folderPath) {
    string zipOutput = @"C:\temp\myoutput.zip";
    try {
        using (ZipOutputStream zs = new ZipOutputStream(File.Create(zipOutput))) {
            zs.SetLevel(9); // 0-9 (9 being best compression)
            foreach (string file in Directory.GetFiles(folderPath)) {
                ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                entry.DateTime = DateTime.Now;
                using (FileStream fs = File.OpenRead(file)) {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    entry.Size = buffer.Length; // This is very important
                    zs.PutNextEntry(entry);
                    zs.Write(buffer, 0, buffer.Length);
                }
            }
            zs.Finish();
            zs.Close();
        }
    }
    catch { throw; }
}

它可以压缩文件夹中的所有文件。

但我想要的是压缩整个文件夹。与该文件夹中的文件夹一样,该文件夹也包含在 zip 文件中。

提前致谢

4

2 回答 2

3

使用FastZip对象。

ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip();
z.CreateEmptyDirectories = true;
z.CreateZip("F:\\ZipTest.zip", "F:\\ZipTest\\", true, ""); 

if (File.Exists("F:\\ZipTest.zip"))
    Console.WriteLine("Done");
else
    Console.WriteLine("Failed");
于 2012-08-24T06:48:24.027 回答
1

我使用以下代码:

public static bool ZipIt(string sourcePath, string destinationPath)
        {          
            List<string> ListOfFiles = GetListOfFiles(sourcePath);
            try
            {
                string OutPath = destinationPath + ".zip";               
                int TrimLength = (Directory.GetParent(sourcePath)).ToString().Length;
                TrimLength += 1;
                //remove '\'
               FileStream ostream;
                byte[] obuffer;               
                ZipOutputStream oZipStream = new  ZipOutputStream(System.IO.File.Create(OutPath));                    
                oZipStream.Password = EncodePassword("Password");
                oZipStream.SetLevel(9);
                // 9 = maximum compression level
                ZipEntry oZipEntry;
                foreach (string Fil in ListOfFiles.ToArray()) // for each file, generate a zipentry
                {
                    oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));    
                    oZipStream.PutNextEntry(oZipEntry);    

                    if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
                    {
                        ostream = File.OpenRead(Fil);    
                        obuffer = new byte[ostream.Length];
                        ostream.Read(obuffer, 0, obuffer.Length);    
                        oZipStream.Write(obuffer, 0, obuffer.Length);    
                        ostream.Close();    
                    }
                }
                oZipStream.Finish();
                oZipStream.Close();               
                return true;
            }
            catch (Exception ex)           
            {    
                return false;
            }
        }            

         public static string EncodePassword(string originalPassword)
         {                         
            Byte[] encodedBytes;  
            encodedBytes = ASCIIEncoding.Default.GetBytes(originalPassword);              
            return BitConverter.ToString(encodedBytes);
         }
于 2012-08-24T07:38:21.703 回答