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;
            }
        }
     
     
  • 相关阅读:
    Android开发--去掉标题栏
    Android开发app如何设定应用图标下的应用名称为汉字以及自定义图标
    mysql的sql其他 SQL中inner join、outer join和cross join的区别
    中文乱码问题 -js页面传值乱码
    liunx Centos Xshell 简单命令汇总
    html 属性及相关应用-实例
    时间格式转换
    三元表达式
    Grid++Report生成简单的条形码、Excel导出、图表控件 等
    jmp指令的简单应用
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/2751564.html
Copyright © 2011-2022 走看看