windows mobile 上文件压缩与解压缩之二

 

Authorquietwalk

Date2010-10-20

 

使用:ICSharpCode.SharpZipLib.Zip

 

using System;

using System.Linq;

using System.Collections.Generic;

using System.Text;

 

using ICSharpCode.SharpZipLib.Zip;//ZipOutputStream

using System.IO;//FileMode

 

//ZipXmlFile()使用中,原来的ZipFile()比较繁琐,不用。

 

namespace quietwalk.ZipUnZip

{

    public class SharpZipHelper

    {

        #region zip

        //需要压缩的XML文件所在的目录

        public static string XmlToZipPath = @"\Storage Card\PDADataExchange\Send\xml\";

 

        //压缩之后的ZIP包所放的目录

        public static string XmlZipedPath = @"\Storage Card\PDADataExchange\Send\zip\";

 

        //获取指定目录下的所有文件名

        public static string[] GetAllXmlFileName()

        {

            try

            {

                DirectoryInfo DirInfo = new DirectoryInfo(XmlToZipPath);

                FileInfo[] files = DirInfo.GetFiles();

                System.Collections.ArrayList lsXmlFileName = new System.Collections.ArrayList();

 

                if (files.Length != 0)

                {

                    for (int i = 0; i < files.Length; i++)

                    {

                        lsXmlFileName.Add( files[i].Name);

                    }

                }

 

                int iCount = lsXmlFileName.Count;

                string[] strXmlFileName = new string[iCount];

                for (int j = 0; j < iCount; j++)

                    strXmlFileName[j] = lsXmlFileName[j].ToString();

 

                return strXmlFileName;

            }

            catch (Exception ex)

            {

                throw (ex);

               // return null;

            }

        }

 

 

        //strZipedFileName:压缩包的名称

        //不设置密码

        //不设置备注

        //不设置压缩比

        public static void ZipXmlFile(string strZipedFileName)

        {

 

            ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(XmlZipedPath + strZipedFileName, FileMode.Create));

 

            //Get all xml file's name.

            string[] fileNamesToZip = GetAllXmlFileName();

            if (fileNamesToZip.Length != 0)

            {

                foreach (string file in fileNamesToZip)

                {

                    FileStream fs = File.OpenRead(XmlToZipPath + file);    //打开待压缩文件  

                    byte[] buffer = new byte[fs.Length];

                    fs.Read(buffer, 0, buffer.Length);      //读取文件流  

                    ZipEntry entry = new ZipEntry(file);    //新建实例  

 

                    entry.DateTime = DateTime.Now;

 

                    entry.Size = fs.Length;

                    fs.Close();

 

                    s.PutNextEntry(entry);

                    s.Write(buffer, 0, buffer.Length);

                }

 

                s.Finish();

                s.Close();

            }

        }

        #endregion zip

 

 

        //********************************************************************        #region UnZip

        //需要解压缩的zip包的路径,包括文件名,从程序中设置

        public static string strZipFilePath;

        public static String ZipFilePath

        {

 

            get { return strZipFilePath; }

            set { strZipFilePath = value; }

        }

 

        //解压缩后的XML文件所放的路径

        public static String UnZipedDir = @"\Storage Card\PDADataExchange\Received\xml\";

 

        //解压缩ZIP文件到指定文件夹

        public static void UnZipFile()  

        {

 

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

            

            try 

            {  

                ZipEntry theEntry;  

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

                {

                    string directoryName = Path.GetDirectoryName(UnZipedDir);// @"\Storage Card\PDADataExchange\Receive\xml\";

                    string pathname = Path.GetDirectoryName(theEntry.Name);  

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

 

                    //生成解压目录   

                    pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  

                    directoryName = directoryName + "\\" + pathname;  

                    Directory.CreateDirectory(directoryName);  

 

                    if (fileName != String.Empty)  

                    {  

                        //解压文件到指定的目录  

                        FileStream streamWriter = File.Create(directoryName + fileName); //directoryName + "\\" + fileName

 

                        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();  

            }  

            catch (Exception eu)  

            {  

                throw eu;  

            }  

            finally 

            {  

                s.Close();  

            }

 

        }

        #endregion UnZip

    }  

}

 

posted @ 2010-10-20 22:34  quietwalk  阅读(352)  评论(0编辑  收藏  举报