zoukankan      html  css  js  c++  java
  • 文件帮助类(解压,压缩)

    using ICSharpCode.SharpZipLib.GZip;
    using ICSharpCode.SharpZipLib.Zip;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace Share
    {
        public class FileHelp
        {
            #region 删除指定路径的文件
            /// <summary>
            /// 删除指定路径的文件
            /// </summary>
            /// <param name="path"></param>
            public static void DeleteFile(string path)
            {
                if (File.Exists(path))//判断文件是不是存在
                {
                    File.Delete(path);//如果存在则删除
                }
            }
    
            /// <summary>
            /// 根据路径删除文件或者文件夹
            /// </summary>
            /// <param name="path"></param>
            public static void DeleteFile2(string path)
            {
                if (!Directory.Exists(path))//如果不存在,则返回
                {
                    return;
                }
                FileAttributes attr = File.GetAttributes(path);
                if (attr == FileAttributes.Directory)
                {
                    Directory.Delete(path, true);
                }
                else
                {
                    File.Delete(path);
                }
            }
            #endregion
    
            #region 压缩文件
            /// <summary>
            /// 压缩文件
            /// </summary>
            /// <param name="filesPath">文件路径</param>
            /// <param name="zipFilePath">压缩路径</param>
            private static void CreateZipFile(string filesPath, string zipFilePath)
            {
    
                if (!Directory.Exists(filesPath))
                {
                    Console.WriteLine("Cannot find directory '{0}'", filesPath);
                    return;
                }
    
                try
                {
                    string[] filenames = Directory.GetFiles(filesPath);
                    using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                    {
    
                        s.SetLevel(9); // 压缩级别 0-9
                        //s.Password = "123"; //Zip压缩文件密码
                        byte[] buffer = new byte[4096]; //缓冲区大小
                        foreach (string file in filenames)
                        {
                            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                            entry.DateTime = DateTime.Now;
                            s.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    s.Write(buffer, 0, sourceBytes);
                                } while (sourceBytes > 0);
                            }
                        }
                        s.Finish();
                        s.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception during processing {0}", ex);
                }
            }
            #endregion
    
            #region 解压
            /// <summary>
            /// 解压,解压到exe目录下
            /// </summary>
            /// <param name="zipFilePath">要解压的文件路径</param>
            public static bool UnZipFile(string zipFilePath, Action<int> proValue,int pos, int posMax)
            {
                if (!File.Exists(zipFilePath))
                {
                    Console.WriteLine("Cannot find file '{0}'", zipFilePath);
                    return false;
                }
    
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        proValue(pos + 1 >= posMax ? pos : pos++);
                        Console.WriteLine(theEntry.Name);
    
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
    
                        // create directory
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(directoryName);
                        }
    
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(theEntry.Name))
                            {
    
                                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;
                                    }
                                }
                            }
                        }
                    }
                }
                return true;
            }
    
            //使用GZIP解压文件的方法
            /// <summary>
            /// 使用GZIP解压文件的方法
            /// </summary>
            /// <param name="zipfilename"></param>
            /// <param name="unzipfilename"></param>
            /// <returns></returns>
            static bool UnGzipFile(string zipfilename, string unzipfilename)
            {
                bool blResult;//表示解压是否成功的返回结果
                //创建压缩文件的输入流实例
                using (GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipfilename)))
                {
                    //创建目标文件的流
                    FileStream destFile = File.Open(unzipfilename, FileMode.Create);
                    try
                    {
                        int buffersize = 2048;//缓冲区的尺寸,一般是2048的倍数
                        byte[] FileData = new byte[buffersize];//创建缓冲数据
                        while (buffersize > 0)//一直读取到文件末尾
                        {
                            buffersize = zipFile.Read(FileData, 0, buffersize);//读取压缩文件数据
                            destFile.Write(FileData, 0, buffersize);//写入目标文件
                        }
                        blResult = true;
                    }
                    catch (Exception ee)
                    {
                        //Console.WriteLine(ee.Message);
                        blResult = false;
                    }
                    destFile.Close();//关闭目标文件
                    zipFile.Close();//关闭压缩文件
                }
                return blResult;
            }
            #endregion
    
            #region 根据文件路径获取string
            public static string GetFileJson(string filepath)
            {
                string json = string.Empty;
                using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
                    {
                        json = sr.ReadToEnd();
                    }
                }
                return json;
            }
            #endregion
        }
    
    }
  • 相关阅读:
    通过线程池,从hbase中拿数据
    phoenix如何压缩表,以及如何映射表
    spring boot改造现有jms activeMQ配置
    windows安装redis
    mysql免安装版配置
    转发和重定向的区别
    object.equals(null)和object==null区别
    用IDEA学习getRealPath遇到的问题
    总误按win+Enter键弹出讲述人
    戴尔电脑插耳机后声音变化问题
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/9020232.html
Copyright © 2011-2022 走看看