• 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;
            }
        }
     
     
  • 相关阅读:
    IntelliJ IDEA 最新注册码
    tidyverse|数据分析常规操作-分组汇总(sumamrise+group_by)
    ComplexHeatmap|根据excel表绘制突变景观图(oncoplot)
    Tidyverse| XX_join :多个数据表(文件)之间的各种连接
    LDheatmap | SNP连锁不平衡图(LD)可视化,自己数据实现版!
    Tidyverse|数据列的分分合合,爱恨情仇
    R-ggpmisc|回归曲线添加回归方程,R2,方差表,香不香?
    R-rbind.fill|列数不一致的多个数据集“智能”合并,Get!
    R|tableone 快速绘制文章“表一”-基线特征三线表
    R|生存分析
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/2751564.html
Copyright © 2020-2023  润新知