利用ICSharpCode.SharpZipLib來實現的壓縮與解壓縮類

iDotNetSpace發表於2009-02-16

最近,在專案中經常需要處理壓縮和解壓縮檔案的操作。在網上找到了相關資料,自己整理了下,寫了兩個類:一個壓縮類;一個解壓縮類。

當然是利用了ICSharpCode.SharpZipLib提供的方法來對檔案壓縮和解壓縮。

壓縮類
///


    /// 壓縮類
    ///

    class ZipClass
    {
        private int compressionLevel = 9;
        private byte[] buffer = new byte[2048];

        ///


        /// 預設建構函式
        ///

        public ZipClass()
        {
        }
        ///
        /// 建構函式
        ///

        /// 緩衝區大小
        /// 壓縮率:0-9
        public ZipClass(int bufferSize, int compressionLevel)
        {
            buffer = new byte[bufferSize];
            this.compressionLevel = compressionLevel;
        }

        ///


        /// 壓縮檔案
        ///

        /// 要壓縮的檔案路徑
        /// 壓縮後的檔案路徑
        public void ZipFile(string fileToZip, string zipedFile)
        {
            if (!File.Exists(fileToZip))
            {
                throw new FileNotFoundException("The specified file " + fileToZip + " could not be found.");
            }

            using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
            {
                string fileName = Path.GetFileName(fileToZip);
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipStream.PutNextEntry(zipEntry);
                zipStream.SetLevel(compressionLevel);

                using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
                {
                    int size = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, size);

                    while (size < streamToZip.Length)
                    {
                        int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                }
            }
        }

        ///


        /// 得到檔案下的所有檔案
        ///

        /// 資料夾路徑
        ///
        public ArrayList GetFileList(string directory)
        {
            ArrayList fileList = new ArrayList();
            bool isEmpty = true;
            foreach (string file in Directory.GetFiles(directory))
            {
                fileList.Add(file);
                isEmpty = false;
            }
            if (isEmpty)
            {
                if (Directory.GetDirectories(directory).Length == 0)
                {
                    fileList.Add(directory + @"/");
                }
            }
            foreach (string dirs in Directory.GetDirectories(directory))
            {
                foreach (object obj in GetFileList(dirs))
                {
                    fileList.Add(obj);
                }
            }
            return fileList;
        }

        ///


        /// 壓縮資料夾
        ///

        /// 要壓縮的資料夾路徑
        /// 壓縮或的資料夾路徑
        public void ZipDerctory(string directoryToZip, string zipedDirectory)
        {
            using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedDirectory)))
            {
                ArrayList fileList = GetFileList(directoryToZip);
                int directoryNameLength = (Directory.GetParent(directoryToZip)).ToString().Length;

                zipStream.SetLevel(compressionLevel);
                ZipEntry zipEntry = null;
                FileStream fileStream = null;

                foreach (string fileName in fileList)
                {
                    zipEntry = new ZipEntry(fileName.Remove(0, directoryNameLength));
                    zipStream.PutNextEntry(zipEntry);

                    if (!fileName.EndsWith(@"/"))
                    {
                        fileStream = File.OpenRead(fileName);
                        fileStream.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }
 

解壓縮類
///


    /// 解壓縮類
    ///

    class UnZipClass
    {
        private byte[] buffer = new byte[2048];

        ///


        /// 建構函式
        ///

        /// 緩衝區大小
        public UnZipClass(int bufferSize)
        {
            buffer = new byte[bufferSize];
        }

        ///


        /// 預設建構函式
        ///

        public UnZipClass()
        {
        }

        ///


        /// 解壓縮檔案
        ///

        /// 壓縮檔案路徑
        /// 解壓縮檔案路徑
        public void UnZipFile(string zipFilePath, string unZipFilePatah)
        {
            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry=zipStream.GetNextEntry()) != null)
                {
                    string fileName = Path.GetFileName(zipEntry.Name);
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipEntry.CompressedSize == 0)
                            break;
                        using (FileStream stream = File.Create(unZipFilePatah + fileName))
                        {
                            while (true)
                            {
                                int size = zipStream.Read(buffer, 0, buffer.Length);
                                if (size > 0)
                                {
                                    stream.Write(buffer, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        ///


        /// 解壓縮目錄
        ///

        /// 壓縮目錄路徑
        /// 解壓縮目錄路徑
        public void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath)
        {
            using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))
            {
                ZipEntry zipEntry = null;
                while ((zipEntry = zipStream.GetNextEntry())!= null)
                {
                    string directoryName = Path.GetDirectoryName(zipEntry.Name);
                    string fileName = Path.GetFileName(zipEntry.Name);

                    if (!string.IsNullOrEmpty(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        if (zipEntry.CompressedSize == 0)
                            break;
                        if (zipEntry.IsDirectory)
                        {
                            directoryName = Path.GetDirectoryName(unZipDirecotyPath + zipEntry.Name);
                            Directory.CreateDirectory(directoryName);
                        }

                        using (FileStream stream = File.Create(unZipDirecotyPath + zipEntry.Name))
                        {
                            while (true)
                            {
                                int size = zipStream.Read(buffer, 0, buffer.Length);
                                if (size > 0)
                                {
                                    stream.Write(buffer, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-553427/,如需轉載,請註明出處,否則將追究法律責任。

相關文章