当前位置: 爱符号 - 所有分类 - 资源共享 - 文档 - C# - C#调用SharpZipLib实现ZIP的加密压缩
C#调用SharpZipLib实现ZIP的加密压缩 C#DiaoYongSharpZipLibShiXianZIPDeJiaMiYaSuo
魏王杰 发表于:2014-09-26 13:26:15 阅读(655)
关键词:SharpZipLib SharpZipLib 加密压缩 加密压缩 zip加密 zip加密
新建一个类:
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Zipper
{
    public class ZipHelper
    {
        //压缩目录
        private static bool ZipDirectory(string directory, ZipOutputStream s, string parentFolder)
        {
            bool res = true;
            string[] folders, filenames;
            ZipEntry entry = null;
            FileStream fs = null;
            Crc32 crc = new Crc32();
            try
            {
                //创建当前文件夹
                entry = new ZipEntry(Path.Combine(parentFolder, Path.GetFileName(directory) + "/")); //加上 “/” 才会当成是文件夹创建
                s.PutNextEntry(entry);
                s.Flush();
                //先压缩文件,再递归压缩文件夹 
                filenames = Directory.GetFiles(directory);
                foreach (string file in filenames)
                {
                    //打开压缩文件
                    fs = File.OpenRead(file);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    entry = new ZipEntry(Path.Combine(parentFolder, Path.GetFileName(directory) + "/" + Path.GetFileName(file)));
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                {
                    entry = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
            folders = Directory.GetDirectories(directory);
            foreach (string folder in folders)
            {
                if (!ZipDirectory(folder, s, Path.Combine(parentFolder, Path.GetFileName(directory))))
                {
                    return false;
                }
            }
            return res;
        }
       //压缩目录
        private static bool ZipDirectory(string diretory, string filename, String password)
        {
            bool res;
            if (!Directory.Exists(diretory))
            {
                return false;
            }
            ZipOutputStream s = new ZipOutputStream(File.Create(filename));
            s.SetLevel(6);//压缩级别
            if (!String.IsNullOrEmpty(password))
                s.Password = password;
            res = ZipDirectory(diretory, s, "");
            s.Finish();
            s.Close();
            return res;
        }
        //压缩文件
        private static bool ZipFile(string filename, string zipName, String password)
        {
            if (!File.Exists(filename))
            {
                return false;
            }
            FileStream zipFile = null;
            ZipOutputStream zipStream = null;
            ZipEntry zipEntry = null;
            bool res = true;
            try
            {
                zipFile = File.OpenRead(filename);
                byte[] buffer = new byte[zipFile.Length];
                zipFile.Read(buffer, 0, buffer.Length);
                zipFile.Close();
                zipFile = File.Create(zipName);
                zipStream = new ZipOutputStream(zipFile);
                if(!String.IsNullOrEmpty(password))
                    zipStream.Password = password;
                zipEntry = new ZipEntry(Path.GetFileName(filename));
                zipStream.PutNextEntry(zipEntry);
                zipStream.SetLevel(6);
                zipStream.Write(buffer, 0, buffer.Length);
            }
            catch
            {
                res = false;
            }
            finally
            {
                if (zipEntry != null)
                {
                    zipEntry = null;
                }
                if (zipStream != null)
                {
                    zipStream.Finish();
                    zipStream.Close();
                }
                if (zipFile != null)
                {
                    zipFile.Close();
                    zipFile = null;
                }
                GC.Collect();
                GC.Collect(1);
            }
            return res;
        }
        /// <summary>
        /// 递归压缩文件夹
        /// </summary>
        /// <param name="directory">待压缩的文件夹</param>
        /// <param name="filename">压缩文件名称</param>
        /// <param name="password">压缩文件密码,为空则不设置密码</param>
        /// <returns>是否压缩成功</returns>
        public static bool Zip(String directory, String filename, String password)
        {
            if (Directory.Exists(directory))
            {
                return ZipDirectory(directory, filename, password);
            }
            else if (File.Exists(directory))
            {
                return ZipFile(directory, filename, password);
            }
            else
            {
                return false;
            }
        }
    }
}

调用:

ZipHelper.Zip("./" + listBox1.Text, "./backupsys_temp" + ".ZIP", "aaaaaa");

声明:以上内容仅代表作者观点,不代表爱符号赞成此内容或立场