zoukankan      html  css  js  c++  java
  • ICSharpCode.SharpZipLib .dll 使用说明

     public class FileUtil
        {
            /// <summary>压缩文件,打包
            ///
            /// </summary>
            /// <param name="strTargetFilePath">压缩包存放地完整路径</param>
            /// <param name="strLocalPath">源路径</param>
            public static bool Zip(string strTargetFilePath, string strLocalPath)
            {
                bool isZipSucess = false;
                string strFileName = string.Empty;
                Crc32 crc = new Crc32();
                try
                {
                    //zip压缩打包
                    using (ZipOutputStream zip = new ZipOutputStream(System.IO.File.Create(strTargetFilePath)))
                    {
                        System.IO.DirectoryInfo zipdir = new System.IO.DirectoryInfo(strLocalPath);
                        System.IO.FileInfo[] zipfiles = zipdir.GetFiles();
                        foreach (System.IO.FileInfo file in zipfiles)
                        {
                            ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file.FullName));

                            using (System.IO.FileStream fs = file.OpenRead())
                            {
                                int sourceBytes;
                                byte[] buffer = new byte[fs.Length];
                                entry.Size = fs.Length;
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                crc.Reset();
                                crc.Update(buffer);
                                entry.Crc = crc.Value;
                                zip.PutNextEntry(entry);
                                zip.Write(buffer, 0, sourceBytes);

                            }
                        }
                        zip.Finish();
                        zip.Close();
                    }
                    isZipSucess = true;
                }
                catch (Exception ex)
                {
                    isZipSucess = false;
                    LogUtil.Error(ex.ToString());
                }
                strFileName = strLocalPath;
                return isZipSucess;
            }

            /// <summary>
            /// 解压文件
            /// </summary>
            /// <param name="strTargetFilePath">目标路径的压缩文件</param>
            /// <param name="strLocalPath">目标路径</param>
            public static bool UnZip(string strTargetFilePath, string directoryName)
            {
                bool isUnZipSucess = false;
                try
                {
                    using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(strTargetFilePath)))
                    {
                        ZipEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string fileName = System.IO.Path.GetFileName(theEntry.Name);
                            // create directory
                            if (directoryName.Length > 0)
                            {
                                System.IO.Directory.CreateDirectory(directoryName);
                            }
                            if (fileName != String.Empty)
                            {
                                string strFilePath = directoryName + "\\" + theEntry.Name;//将文件解压缩到指定的目录
                                using (System.IO.FileStream streamWriter = System.IO.File.Create(strFilePath))
                                {
                                    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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    isUnZipSucess = true;
                }
                catch (Exception ex)
                {
                    LogUtil.Error(ex.ToString());
                    isUnZipSucess = false;
                }
                return isUnZipSucess;
            }
        }
     
     
  • 相关阅读:
    [C++ Primer Plus] 第9章、内存模型和名称空间(二)课后习题
    [C++ Primer Plus] 第9章、内存模型和名称空间(一)程序清单
    [c/c++] programming之路(28)、结构体存储和内存对齐+枚举类型+typedef+深拷贝和浅拷贝
    [c/c++] programming之路(27)、union共用体
    [c/c++] programming之路(26)、结构体
    opencv学习之路(37)、运动物体检测(二)
    [Python]基础教程(4)、Python 变量类型
    opencv学习之路(36)、运动物体检测(一)
    opencv学习之路(35)、SURF特征点提取与匹配(三)
    opencv学习之路(34)、SIFT特征匹配(二)
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/2751564.html
Copyright © 2011-2022 走看看