c# 壓縮文件

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;


namespace NurunChina.Utility
{
   
    public class NurunZipper : IDisposable
    {
        private string m_FolderToZIP;
        private List<string> m_FileNamesToZIP;
        private string m_FileNameZipped;
       
        private ZipOutputStream m_ZipStream = null;
        private Crc32 m_Crc;

#region Begin for Public Properties
        /// <summary>
        /// Folder Name to ZIP
        /// Like "C:Test"
        /// </summary>
        public  string FolderToZIP
        {
            get { return m_FolderToZIP; }
            set { m_FolderToZIP = value; }
        }

        /// <summary>
        /// File Name to ZIP
        /// Like "C:TestTest.txt"
        /// </summary>
        public List<string> FileNamesToZIP
        {
            get { return m_FileNamesToZIP; }
            set { m_FileNamesToZIP = value; }
        }

        /// <summary>
        /// Zipped File Name
        /// Like "C:TestMyZipFile.ZIP"
        /// </summary>
        public  string FileNameZipped
        {
            get { return m_FileNameZipped; }
            set { m_FileNameZipped = value; }
        }
#endregion

        /// <summary>
        /// Zip single file
        /// </summary>
        /// <param name="SourceFile"></param>
        /// <param name="ZippedFile"></param>
        public static void ZipOneFile(string SourceFile, string ZippedFile)
        {
            NurunZipper nz = new NurunZipper();
            List<string> al = new List<string>();

            al.Add(SourceFile);

            nz.m_FileNamesToZIP = al;
            nz.FileNameZipped = ZippedFile;

            nz.ZipFiles();
            nz.Dispose();
        }

        /// <summary>
        /// Zip multiple files
        /// </summary>
        /// <param name="SourceFile"></param>
        /// <param name="ZippedFile"></param>
        public static void ZipMultipleFile(List<string> SourceFiles, string ZippedFile)
        {
            NurunZipper nz = new NurunZipper();

            nz.m_FileNamesToZIP = SourceFiles;
            nz.FileNameZipped = ZippedFile;

            nz.ZipFiles();
            nz.Dispose();
        }

        /// <summary>
        /// The construct
        /// </summary>
        public NurunZipper()
        {
            this.m_FolderToZIP = "";
            this.m_FileNamesToZIP = new List<string>();
            this.m_FileNameZipped = "";
        }

#region ZipFolder
        /// <summary>
        /// Zip one folder : single level
        /// Before doing this event, you must set the Folder and the ZIP file name you want
        /// </summary>
        public void ZipFolder()
        {
            if (this.m_FolderToZIP.Trim().Length == 0)
            {
                throw new Exception("You must setup the folder name you want to zip!");
            }

            if(Directory.Exists(this.m_FolderToZIP) == false)
            {
                throw new Exception("The folder you input does not exist! Please check it!");
            }
           
            if (this.m_FileNameZipped.Trim().Length == 0)
            {
                throw new Exception("You must setup the zipped file name!");
            }
           
            string[] fileNames = Directory.GetFiles(this.m_FolderToZIP.Trim());

            if (fileNames.Length == 0)
            {
                throw new Exception("Can not find any file in this folder(" + this.m_FolderToZIP + ")!");
            }

            // Create the Zip File
            this.CreateZipFile(this.m_FileNameZipped);

            // Zip all files
            foreach(string file in fileNames)
            {
                this.ZipSingleFile(file);
            }

            // Close the Zip File
            this.CloseZipFile();
        }
#endregion

#region ZipFiles
        /// <summary>
        /// Zip files
        /// Before doing this event, you must set the Files name and the ZIP file name you want
        /// </summary>
        public void ZipFiles()
        {
            if (this.m_FileNamesToZIP.Count == 0)
            {
                throw new Exception("You must setup the files name you want to zip!");
            }

            foreach(string file in this.m_FileNamesToZIP)
            {
                if(File.Exists((file).Trim()) == false)
                {
                    throw new Exception("The file(" + (string)file + ") you input does not exist! Please check it!");
                }
            }

            if (this.m_FileNameZipped.Trim().Length == 0)
            {
                throw new Exception("You must input the zipped file name!");
            }

            // Create the Zip File
            this.CreateZipFile(this.m_FileNameZipped);

            // Zip this File
            foreach(object file in this.m_FileNamesToZIP)
            {
                this.ZipSingleFile((string)file);
            }

            // Close the Zip File
            this.CloseZipFile();
        }
#endregion

#region CreateZipFile
        /// <summary>
        /// Create Zip File by FileNameZipped
        /// </summary>
        /// <param name="fileNameZipped">zipped file name like "C:TestMyZipFile.ZIP"</param>
        private void CreateZipFile(string fileNameZipped)
        {
            this.m_Crc = new Crc32();
            this.m_ZipStream = new ZipOutputStream(File.Create(fileNameZipped));
            this.m_ZipStream.SetLevel(6); // 0 - store only to 9 - means best compression
        }
#endregion

#region CloseZipFile
        /// <summary>
        /// Close the Zip file
        /// </summary>
        private void CloseZipFile()
        {
            this.m_ZipStream.Finish();
            this.m_ZipStream.Close();
            this.m_ZipStream = null;
        }
#endregion

#region ZipSingleFile
        /// <summary>
        /// Zip single file
        /// </summary>
        /// <param name="fileName">file name like "C:TestTest.txt"</param>
        private void ZipSingleFile(string fileNameToZip)
        {
            // Open and read this file
            FileStream fso = File.OpenRead(fileNameToZip);

            int BufferSize = 5000;
            int ReadSize = 0;
            // Read this file to Buffer
            //byte[] buffer = new byte[fso.Length];
            byte[] buffer = new byte[BufferSize];
            //fso.Read(buffer,0,buffer.Length);

            // Create a new ZipEntry
            string [] names = fileNameToZip.Split('//');
            ZipEntry zipEntry = new ZipEntry(names[names.Length - 1]);
           
            zipEntry.DateTime = DateTime.Now;
            // set Size and the crc, because the information
            // about the size and crc should be stored in the header
            // if it is not set it is automatically written in the footer.
            // (in this case size == crc == -1 in the header)
            // Some ZIP programs have problems with zip files that don't store
            // the size and crc in the header.
            zipEntry.Size = fso.Length;

            // Using CRC to format the buffer
            this.m_Crc.Reset();

            fso.Seek(0, SeekOrigin.Begin);
            do
            {
                ReadSize = fso.Read(buffer, 0, BufferSize);
                this.m_Crc.Update(buffer, 0, ReadSize);
            } while (ReadSize > 0);
           
            zipEntry.Crc = this.m_Crc.Value;

            // Add this ZipEntry to the ZipStream
            this.m_ZipStream.PutNextEntry(zipEntry);

            fso.Seek(0, SeekOrigin.Begin);
            do
            {
                ReadSize = fso.Read(buffer, 0, BufferSize);
                this.m_ZipStream.Write(buffer, 0, ReadSize);
            } while (ReadSize > 0);
           

            fso.Close();
            fso = null;
        }
#endregion

#region IDisposable member

        /// <summary>
        /// Release all objects
        /// </summary>
        public void Dispose()
        {
            if(this.m_ZipStream != null)
            {
                this.m_ZipStream.Close();
                this.m_ZipStream = null;
            }
        }

#endregion
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章