using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Collections;

using ICSharpCode.SharpZipLib.Zip;

 

namespace ZIP

{

    public static class ZipUtil

    {

        private static ArrayList GenerateFileList(string Dir)

        {

            ArrayList fils = new ArrayList();

            bool empty = true;

            foreach (string file in Directory.GetFiles(Dir))

            {

                fils.Add(file);

                empty = false;

            }

            if (empty)

            {

                //if directory is completely empty add it

                if (Directory.GetDirectories(Dir).Length == 0)

                    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

        }

 

        public static void ZipFiles(string inputFolderPath, string outputPathAndFile,

            string password)

        {

            ArrayList ar = GenerateFileList(inputFolderPath);//generate file list

            int trimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;

            //trimLength += 1;//remove '\'

            FileStream oStream;

            byte[] oBuffer;

            string outPath = inputFolderPath + @"\" + outputPathAndFile;

            //create zip stream

            ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath));

            if (string.IsNullOrEmpty(password))

                oZipStream.Password = password;

            oZipStream.SetLevel(9);//maximum compression

            ZipEntry oZipEntry;

            foreach (string Fil in ar)

            {

                oZipEntry = new ZipEntry(Fil.Remove(0, trimLength));

                oZipStream.PutNextEntry(oZipEntry);

                if (!Fil.EndsWith(@"/"))//if file ends with '/' it's 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();

        }

 

        public static void UnZipFiles(string zipPathAndFile, string outputFolder,

            string password, bool deleteZipFile)

        {

            ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));

            if (string.IsNullOrEmpty(password))

                s.Password = password;

            ZipEntry theEntry;

            string tmpEntry = string.Empty;

            while( (theEntry = s.GetNextEntry()) != null)

            {

                string directoryName = outputFolder;

                string fileName = Path.GetFileName(theEntry.Name);

                if (directoryName != string.Empty) //create directory

                    Directory.CreateDirectory(directoryName);

                if (fileName != string.Empty)

                {

                    if (theEntry.Name.IndexOf(".ini") < 0)

                    {

                        string fullPath = directoryName + "\\" + theEntry.Name;

                        fullPath = fullPath.Replace("\\ ", "\\");

                        string fullDirPath = Path.GetDirectoryName(fullPath);

                        if (!Directory.Exists(fullDirPath))

                            Directory.CreateDirectory(fullDirPath);

                        FileStream streamWriter = File.Create(fullPath);

                        int size = 2048;

                        byte[] data = new byte[2048];

                        while (true)

                        {

                            size = s.Read(data, 0, data.Length);

                            if (size > 0)

                                streamWriter.Write(data, 0, size);

                            else

                                break;

                            streamWriter.Close();

                        }

                    }

                }

            }

            s.Close();

            if (deleteZipFile)

                File.Delete(zipPathAndFile);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            //compression

            ZipUtil.ZipFiles(@"C:\test", "test1.zip", "123456");

            //uncompression

            //ZipUtil.UnZipFiles(@"C:\test\test1.zip", @"C:\test", "123456", false);

 

        }

 

    }

}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Big Bear 的頭像
    Big Bear

    Programs Knowledge

    Big Bear 發表在 痞客邦 留言(0) 人氣()