C# 压缩解压缩,支持密码

private int ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
        {
            if (!System.IO.File.Exists(FileToZip))
            {
                return -1;
                //throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
            }

            System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            ZipEntry ZipEntry = new ZipEntry("ZippedFile");
            ZipStream.PutNextEntry(ZipEntry);
            ZipStream.SetLevel(CompressionLevel);
            byte[] buffer = new byte[BlockSize];
            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
            ZipStream.Write(buffer, 0, size);
            try
            {
                while (size < StreamToZip.Length)
                {
                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                    ZipStream.Write(buffer, 0, sizeRead);
                    size += sizeRead;
                }                
            }
            catch (System.Exception ex)
            {
                return -2;
                //throw ex;
            }
            ZipStream.Finish();
            ZipStream.Close();
            StreamToZip.Close();

            return 0;
        }

        public int Zip(string file1, string file2, string password)
        {
            try
            {
                ZipOutputStream s = new ZipOutputStream(File.Create(file2));

                s.SetLevel(6); // 0 - store only to 9 - means best compression 

                s.Password = encrypt(password);

                
                    //打开压缩文件 
                    FileStream fs = File.OpenRead(file1);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);

                    Array arr = file1.Split('\\');
                    string le = arr.GetValue(arr.Length - 1).ToString();
                    ZipEntry entry = new ZipEntry(le);
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                
                s.Finish();
                s.Close();

                return 0;
            }
            catch (System.Exception ex)
            {
                return -2;
                //throw ex;
            }
        }

        public void UnZip(string file1, string file2, string password)
        {
            FileStream fileStreamIn = new FileStream(file1, FileMode.Open, FileAccess.Read);
            using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
            {
                zipInStream.Password = encrypt(password);
                ZipEntry entry = zipInStream.GetNextEntry();
                do
                {
                    using (FileStream fileStreamOut = new FileStream(file2, FileMode.Create, FileAccess.Write))
                    {

                        int size = 2048;
                        byte[] buffer = new byte[2048];
                        do
                        {
                            size = zipInStream.Read(buffer, 0, buffer.Length);
                            fileStreamOut.Write(buffer, 0, size);
                        } while (size > 0);
                    }
                } while ((entry = zipInStream.GetNextEntry()) != null);
            }
        }

        #region "MD5加密"
        /// <summary>
        ///32位 MD5加密
        /// </summary>
        /// <param name="str">加密字符</param>
        /// <returns></returns>
        private static string encrypt(string str)
        {
            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
        #endregion

 

posted @ 2017-12-26 15:19  都是城市惹的祸  阅读(1622)  评论(0)    收藏  举报