C# 如何使用ICSharplib使用加密密码压缩文件夹

C# 如何使用ICSharplib使用加密密码压缩文件夹,c#,.net,C#,.net,如何使用ICSharplib压缩文件夹。 有没有什么方法可以在压缩密码时添加加密密码? 没有选择,我可以使用任何其他dll。只能使用ICSharplib 目前我正在使用这个代码块 private static void CompressFiles(string folderPath) { string zipOutput = @"C:\temp\myoutput.zip"; try { using (ZipOutputStream zs = new ZipOutp

如何使用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文件中

提前感谢

使用FastZip对象

使用FastZip对象

我使用以下代码:

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);
         }
我使用以下代码:

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

有什么方法可以在压缩密码的同时添加一个加密密码吗?添加这样的密码z.password=VerySecret!!;我在z.CreateZipF:\\ZipTest.zip之前尝试过z.Password=test,F:\\ZipTest\\,true;不工作!!!嗯,看来我弄错了,对不起。也许FastZip不支持它。我会检查它,同时你可以在这里挖掘高级功能:这样你可以添加密码,甚至更好的加密。我认为还有一些代码可以添加目录。让我检查一下FastZip对象的密码支持。我已经检查了帮助文件,FastZip应该有一个密码属性。到底是什么不起作用?有没有办法在压缩密码的同时添加一个加密密码?添加这样的密码z.password=VerySecret!!;我在z.CreateZipF:\\ZipTest.zip之前尝试过z.Password=test,F:\\ZipTest\\,true;不工作!!!嗯,看来我弄错了,对不起。也许FastZip不支持它。我会检查它,同时你可以在这里挖掘高级功能:这样你可以添加密码,甚至更好的加密。我认为还有一些代码可以添加目录。让我检查一下FastZip对象的密码支持。我已经检查了帮助文件,FastZip应该有一个密码属性。到底是什么不起作用?有没有办法在压缩时添加加密密码?有没有办法在压缩时添加加密密码?为什么要在列表中调用ToArray只是为了在foreach中使用?似乎有点多余。你为什么要在列表上打电话给ToArray只是为了在foreach中使用?看起来有点多余。