簡體   English   中英

如何使用ICSharplib使用加密密碼壓縮文件夾

[英]How to Zip a folder with encrypt password using ICSharplib

如何使用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對象。

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

我使用以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM