• 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

        }  

    }

     

  • 相关阅读:
    [POI2005]A Journey to Mars 单调队列
    滑动窗口 单调队列
    逆序对 模拟贪心
    迷宫 dfs爆搜
    [Usaco2019 Feb]The Great Revegetation
    [Usaco2007 Dec]挑剔的美食家
    [HNOI2004]宠物收养所
    bzoj2639 矩形计算
    [Ahoi2013]作业
    Gty的二逼妹子序列
  • 原文地址:https://www.cnblogs.com/quietwalk/p/1857095.html
Copyright © 2020-2023  润新知