Csharp 利用ICSharpCode.SharpZipLib解壓文件

/*
 *http://www.koders.com/csharp/fid7241B3C8598C20BA18652B5BB5C19D220988E2D4.aspx?s=file
 * http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
 * http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx
 * http://www.eggheadcafe.com/community/csharp/2/10060149/zip-a-file-using-icsharpcodesharpziplibzip.aspx
 * http://www.eggheadcafe.com/tutorials/csharp/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-files-with-c.aspx
 *
 */

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

namespace CompressFolders
{
    /// <summary>
    /// 20120530
    /// 涂聚文 Geovin Du
    /// </summary>
    public partial class Form1 : Form
    {
        string strBaseDir = "";
        /// <summary>
        ///
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //指定文件
            //this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip";
            //this.saveFileDialog1.Title = "保存文件";
            //this.saveFileDialog1.FileName = "数据备份";
            //this.saveFileDialog1.InitialDirectory =Application.StartupPath;
            //this.saveFileDialog1.RestoreDirectory = true;
            //string Fname = "";
            //if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    string[] filenames ={ "Properties", "Form1.resx", "frmMain.Designer.cs", "Program.cs", "Form1.Designer.cs", "Form1.resx", "frmMain.cs" };//可以自己定义所压缩的文件
            //    Fname = this.saveFileDialog1.FileName;
            //    ZipOutputStream s = new ZipOutputStream(File.Create(Fname));//创建压缩文件的目录及名称
            //    s.Password = "geovindu";//设置压缩文件的密码
            //    s.SetLevel(6);// 0 - store only to 9 - means best compression
            //    foreach (string file in filenames)
            //    {
            //        if (Directory.Exists(Application.StartupPath + @"/" + file))
            //        {
            //            strBaseDir = Application.StartupPath + @"/" + file + @"/";//压缩文件夹
            //            addZipEntry(strBaseDir, s);
            //        }
            //        else
            //        {
            //            FileStream fs = File.OpenRead(Application.StartupPath + @"/" + file);//打开文件读取
            //            byte[] buffer = new byte[fs.Length];
            //            fs.Read(buffer, 0, buffer.Length);
            //            string strEntryName = file;
            //            ZipEntry entry = new ZipEntry(strEntryName);
            //            s.PutNextEntry(entry);
            //            s.Write(buffer, 0, buffer.Length);
            //            fs.Close();
            //        }
            //    }
            //    s.Finish();
            //    s.Close();
            //}

        }
        /// <summary>
        /// 壓縮文件夾
        /// </summary>
        /// <param name="PathStr"></param>
        /// <param name="s"></param>
        private void addZipEntry(string PathStr, ZipOutputStream s)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName, s);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                if (item.FullName.Replace(strBaseDir, "") != "Thumbs.db")
                {
                    FileStream fs = File.OpenRead(item.FullName);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string strEntryName = item.FullName.Replace(strBaseDir, "");
                    strEntryName = @"Properties/" + strEntryName;
                    ZipEntry entry = new ZipEntry(strEntryName);
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                    fs.Close();
                }
            }
        }
        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="zipfile"></param>
        /// <param name="directory"></param>
        private void Unzip(string zipfile, string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            ZipInputStream zis = new ZipInputStream(File.OpenRead(zipfile));
            string str = zis.Password;
            zis.Password = "塗聚文";
            ZipEntry theEntry = null;
            while ((theEntry = zis.GetNextEntry()) != null)
            {
                ZipConstants.DefaultCodePage = 936;
                string directoryName = Path.GetDirectoryName(theEntry.Name);//directoryName=null
                string fileName = Path.GetFileName(theEntry.Name);

                if (directoryName != string.Empty)
                {
                    if ((directoryName == "Properties") && (!Directory.Exists(directory + @"/" + directoryName)))
                    {
                        Directory.CreateDirectory(directory + @"/" + directoryName);
                    }
                }
                if (fileName != string.Empty)
                {
                    FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = zis.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            zis.Close();
        }

        /// <summary>
        ///壓縮 100M大文件進程不行
        /// 塗聚文 Geovin Du
        /// </summary>
        /// <param name="inputFolderPath"></param>
        /// <param name="outputPathAndFile"></param>
        /// <param name="password"></param>
        public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
        {
            ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
            int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
            // find number of chars to remove     // from orginal file path
            TrimLength += 1; //remove '\'
            FileStream ostream;
            byte[] obuffer;
            string outPath = inputFolderPath + @"\" + outputPathAndFile;
            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
            if (password != null && password != String.Empty)
                oZipStream.Password = password;
            oZipStream.SetLevel(9); // maximum compression
            ZipEntry oZipEntry;
            foreach (string Fil in ar) // 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);
                }
            }
            oZipStream.Finish();
            oZipStream.Close();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="Dir"></param>
        /// <returns></returns>
        public static ArrayList GenerateFileList(string Dir)
        {
            ArrayList fils = new ArrayList();
            bool Empty = true;
            foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
            {
                fils.Add(file);
                Empty = false;
            }

            if (Empty)
            {
                if (Directory.GetDirectories(Dir).Length == 0)
                // if directory is completely empty, add it
                {
                    fils.Add(Dir + @"/");
                }
            }

            foreach (string dirs in Directory.GetDirectories(Dir)) // recursive
            {
                foreach (object obj in GenerateFileList(dirs))
                {
                    fils.Add(obj);
                }
            }
            return fils; // return file list
        }

        /// <summary>
        /// 打開選擇文件夾
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show(fbd.SelectedPath);
                this.textBox1.Text = fbd.SelectedPath;
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSave_Click(object sender, EventArgs e)
        {
            ZipFiles(this.textBox1.Text.Trim(), this.textBox2.Text.Trim(), "geovindu");

        }
        /// <summary>
        /// 選擇文件夾
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show(fbd.SelectedPath);
                this.textBox2.Text = fbd.SelectedPath;
            }
        }

    }
}
时间: 2024-11-05 14:47:21

Csharp 利用ICSharpCode.SharpZipLib解壓文件的相关文章

C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩

ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定目录下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩logs目录下日志 public static void CompresslogDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic =

C#下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩和解压缩

1.压缩某个指定目录下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩logs目录下日志 public static void CompresslogDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic = new DirectoryInfo(logFilePath);

请问,如何利用ICSharpCode.SharpZipLib将SQLSERVER image数据字段中的压缩文件取出来解压 GZIP

问题描述 如题! 解决方案 解决方案二:先还原成一个.zip的文件,然后再对.zip的文件解压~解决方案三:读取数据库还原为zip文件,放到临时目录里面,再解压.解决方案四:gz

C#利用SharpZipLib解压或压缩文件夹实例操作

最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,"Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] "点击Download可以下载,

利用ICSharpCode压缩打包文件

因为项目需要打包文件,就在同事的建议下用ICSharpCode写了个打包函数.ICSharpCode的介绍就不说了.具体请到官方网站 http://www.icsharpcode.net/ 上了解. 首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar 代码实现多文件,自定义文件,整目录打包等功能.好了..奉上代码: 压缩打包代码 /// <sum

音频-利用libmad怎么解得MP3音乐文件的时长?

问题描述 利用libmad怎么解得MP3音乐文件的时长? 我现在想在android项目中利用libmad对MP3文件进行解码,请问有人用过这个吗?如何解得MP3音乐文件的时长? 解决方案 http://blog.csdn.net/ahyswang/article/details/7748344

C#使用ICSharpCode.SharpZipLib压缩文件[转]

本文来自CSDN博客,出处:http://blog.csdn.net/venus0314/archive/2006/09/25/1280610.aspx   一直以来都是采用WinZIP进行压缩的,调用起来方便,而且公司也有版权,所以就没有考虑过其他的东东.不过唯一不足的地方就是需要安装(包括Win Zip和其Command Line Addon),而且需要让程序知道调用的WinZIP路径,配置起来不是很方便. 本次项目,考虑到程序的易配置性,决定采用另外别的方式进行压缩,找了找就找到了ICSh

vb.net-VB.net如何解压文件?

问题描述 VB.net如何解压文件? 要求: 参数:压缩文件路径,解压后的文件输出目录 返回值:布尔值,是否成功解压 所有路径已经确保正常,都可以正常读写 就求一个解压文件的代码啊啊啊啊qwq 解决方案 如果是VB11.0以上,直接用System.IO.Compression,使用方法和直接读写文件一样http://blog.163.com/lyz_sea/blog/static/115586707201071462730785/ 也可以调用WinRAR的命令行解压缩.http://blog.s

基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// <summary> /// 压缩 /// </summary> /// <param name="sourceDirectory"></param> /// <param name="targetZipName"&g