C# 压缩 ZIP文件

来源:互联网 发布:mix软件怎么让腿瘦 编辑:程序博客网 时间:2024/04/30 14:00

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;
using System.IO;

namespace zip
{
    public partial class Form1 : Form
    {

        private string zipfilecreatename;
        private string filesdirectorypath;
        private int dirnamelength = 0;
        private int ziplevel = 6;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            zipfilecreatename = System.IO.Directory.GetCurrentDirectory().ToString() + @"/ziptemp.zip";
            filesdirectorypath = System.IO.Directory.GetCurrentDirectory().ToString()+@"/ziptemp";
            this.ZipFileCreate(); 
        }

        ///
        /// 压缩文件的方法
        ///
        public void ZipFileCreate()
        {
            ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(zipfilecreatename));
            zipoutputstream.SetLevel(ziplevel);
            Crc32 crc = new Crc32();
            Hashtable fileList = this.getAllFies();
            foreach (DictionaryEntry item in fileList)
            {
                FileStream fs = File.OpenRead(item.Key.ToString());
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(this.filesdirectorypath.Length - this.dirnamelength));
                entry.DateTime = (DateTime)item.Value;
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                zipoutputstream.PutNextEntry(entry);
                zipoutputstream.Write(buffer, 0, buffer.Length);
            }
            zipoutputstream.Finish();
            zipoutputstream.Close();
        }


        //private void Button1_Click(object sender, System.EventArgs e)
        //{

        //    //压缩
        //    filezipcreate.ZipFileCreateName = Server.MapPath(DateTime.Now.ToString("yyyyMMddmmss") + ".zip");
        //    filezipcreate.FileDirectoryPath = Server.MapPath("../Admin/Css");
        //    filezipcreate.ZipFileCreate();
        //}

        //private void Button2_Click(object sender, System.EventArgs e)
        //{

        //    //解压
        //    UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
        //    uzipfilescreate.ZipFileName = this.File1.PostedFile.FileName.Trim();
        //    uzipfilescreate.filesCreatePath = Server.MapPath("");
        //    uzipfilescreate.UZipCreateFiles();
        //}

        ///
        /// 获取所有文件
        ///
        ///
        private Hashtable getAllFies()
        {
            Hashtable FilesList = new Hashtable();
            DirectoryInfo fileDire = new DirectoryInfo(this.filesdirectorypath);
            if (!fileDire.Exists)
            {
                throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");
            }
            this.dirnamelength = fileDire.Name.Length;
            this.getAllDirFiles(fileDire, FilesList);
            this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);
            return FilesList;
        }
         ///
         /// 获取一个文件夹下的所有文件夹里的文件
         ///
        private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)
        {
            foreach (DirectoryInfo dir in dirs)
            {
                foreach (FileInfo file in dir.GetFiles("*.*"))
                {
                    filesList.Add(file.FullName, file.LastWriteTime);
                }
                this.getAllDirsFiles(dir.GetDirectories(), filesList);
            }
        }

        //获取一个文件夹下的文件

        private void getAllDirFiles(DirectoryInfo dir, Hashtable filesList)
        {
            foreach (FileInfo file in dir.GetFiles("*.*"))
            {
                filesList.Add(file.FullName, file.LastWriteTime);
            }
        }  
    }
}

原创粉丝点击