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;
            }
        }
     
     
  • 相关阅读:
    ZOJ 1060 Count the Color
    POJ 3321 Apple Tree
    数字三角形模型
    静态维护区间加等差数列的求和问题
    Codeforces Round #622 (Div. 2)-题解
    算法竞赛进阶指南0x00-算法基础
    Codeforces Round #628 (Div. 2)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)
    Codeforces Round #621 (Div. 1 + Div. 2)
    Codeforces Round #620 (Div. 2) 题解
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/2751564.html
Copyright © 2011-2022 走看看