zoukankan      html  css  js  c++  java
  • 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

        }  

    }

     

  • 相关阅读:
    Vue基础知识总结(一)
    D3.js系列——布局:弦图和集群图/树状图
    D3.js系列——布局:饼状图和力导向图
    D3.js系列——交互式操作和布局
    SQLServer调试
    SQL Server性能常用语句
    sqlserver索引
    从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
    EntityFrame6在本地可以正常使用,部署到IIS后报异常(Additional information: The underlying provider failed on Open.)
    从对象创建和引用小议解耦
  • 原文地址:https://www.cnblogs.com/quietwalk/p/1857095.html
Copyright © 2011-2022 走看看