• 解压缩.zip文件


        /// <summary>
        /// 解压文件
        /// </summary>
        public class UnZipFile
        {
            private string _fileName;
            public string FileName
            {
                get
                {
                    return _fileName;
                }
                set
                {
                    _fileName = value;
                }
            }
            /// <summary>
            /// 获取解压文件的进度
            /// </summary>
            /// <param name="CurrentUnZipFileLength">当前文件解压的流长</param>
            /// <param name="length">当前解压文件的总长度</param>
            public delegate void GetUnZipFileProgress(int CurrentUnZipFileLength,long length);
    
            /// <summary>
            /// 获取解压文件的进度事件
            /// </summary>
            public event GetUnZipFileProgress GetUnZipFileProgressEvent;
            
            /// <summary>
            /// 解压文件
            /// </summary>
            /// <param name="args">下标为0,是需要解压的源文件,下标为1,是解压后的路径</param>
            public void UnZip(string[] args)
            {
                FileStream fileStream = File.OpenRead(args[0]);
                //获取源文件的长度
                long length = fileStream.Length;
                
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream);
               
                ZipEntry theEntry;
                int readlength = 0;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(args[1]);
                    string fileName = Path.GetFileName(theEntry.Name);
                    _fileName = theEntry.Name;
                    try
                    {
                        if (fileName.Length == 0)
                        {
                            continue;
                        }
                        //生成解压目录 
                        
                        if (directoryName==null)
                        {
                            Directory.CreateDirectory(args[1] + theEntry.Name.Replace(fileName, ""));
                        }
                        else
                        {
                            Directory.CreateDirectory(directoryName + "\\" + theEntry.Name.Replace(fileName, ""));
                        }
                        if (fileName != String.Empty)
                        {
                            //解压文件到指定的目录 
                            FileStream streamWriter = File.Create(args[1] + 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);
                                    readlength += size;
                                    if(readlength >=length)
                                    {
                                        readlength = (int)length;
                                    }
                                    if (GetUnZipFileProgressEvent!=null)
                                    {
                                        GetUnZipFileProgressEvent(readlength,length);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            streamWriter.Flush();
                            streamWriter.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        fileStream.Close();
                        s.Close();
                        throw ex;
                        //Log.Writer.WriteLog(ex);
                    }
                }
                //s.Flush();
                fileStream.Close();
                s.Close();
            }
    
    
            /// <summary>
            /// 从 Zip 包中提取指定的文件
            /// </summary>
            /// <param name="zipFilename">Zip压缩包</param>
            /// <param name="filename">要提取的文件</param>
            /// <param name="directoryName">提取后存储的位置</param>
            public void ExtractFile(string zipFilename, String filename, String directoryName)
            {
                FileStream fileStream = File.Open(zipFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                //获取源文件的长度
                long length = fileStream.Length;
                if (length <= 0)
                    return;
    
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream);
    
                ZipEntry theEntry;
                int readlength = 0;
                while ((theEntry = s.GetNextEntry()) != null)
                {
    
                    _fileName = theEntry.Name;
                    
                    string zipEntryName = Path.GetFileName(theEntry.Name);
                    if (filename.Equals(theEntry.Name))
                    {
    
                        try
                        {
    
                            if (zipEntryName != String.Empty)
                            {
                                //解压文件到指定的目录 
                                FileStream streamWriter = File.Create(System.IO.Path.Combine(directoryName, zipEntryName));
                                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);
                                        readlength += size;
                                        if (readlength >= length)
                                        {
                                            readlength = (int)length;
                                        }
                                        if (GetUnZipFileProgressEvent != null)
                                        {
                                            GetUnZipFileProgressEvent(readlength, length);
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                streamWriter.Flush();
                                streamWriter.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            fileStream.Close();
                            s.Close();
                            throw ex;
                            //Log.Writer.WriteLog(ex);
                        }
                        break;
                    }
    
                }//end while
    
                //s.Flush();
                fileStream.Close();
                s.Close();
            }//end method
    
            /// <summary>
            /// 解压指定文件夹(但是此文件夹不能嵌套文件夹)
            /// </summary>
            /// <param name="args">下标为0,是需要解压的源文件,下标为1,是解压后的路径</param>
            /// <param name="folderName">指定文件夹名称</param>
            public void UnZipSpecifiedFolder(string[] args, string folderName,string serverId)
            {
                FileStream fileStream = File.OpenRead(args[0]);
                //获取源文件的长度
                long length = fileStream.Length;
    
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream);
    
                ZipEntry theEntry;
                int readlength = 0;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(args[1]);
                    string theEntryName = null;
                    if (serverId!=null&&serverId!="")
                    {
                        theEntryName = theEntry.Name.Replace("/"+serverId,"");
                    }
                    else
                    {
                        theEntryName = theEntry.Name;
                    }
                   
                    string fileName = Path.GetFileName(theEntryName);
                    _fileName = theEntry.Name;
                    try
                    {
                        if (fileName.Length == 0)
                        {
                            continue;
                        }
                        string str = Path.
                        GetDirectoryName(theEntry.Name.Replace(fileName, ""));
                        string strs = Path.GetDirectoryName(theEntry.Name.Replace(fileName, "")).Replace(serverId + "\\","");
                        if (Path.GetDirectoryName(theEntry.Name.Replace(fileName, "")).Replace(serverId+"\\","") != folderName)
                        {
                            continue;
                        }
                        //生成解压目录 
                        if (directoryName == null)
                        {
                            Directory.CreateDirectory(args[1] + theEntry.Name.Replace(fileName, ""));
                        }
                        else
                        {
                            Directory.CreateDirectory(directoryName + "\\" + theEntry.Name.Replace(fileName, ""));
                        }
                        if (fileName != String.Empty)
                        {
                            //解压文件到指定的目录 
                            FileStream streamWriter = File.Create(args[1] + 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);
                                    readlength += size;
                                    if (readlength >= length)
                                    {
                                        readlength = (int)length;
                                    }
                                    if (GetUnZipFileProgressEvent != null)
                                    {
                                        GetUnZipFileProgressEvent(readlength, length);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            streamWriter.Flush();
                            streamWriter.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                        //Log.Writer.WriteLog(ex);
                    }
                }
                //s.Flush();
                fileStream.Close();
                s.Close();
            }
    
            /// <summary>
            /// 解压制定文件夹或文件
            /// </summary>
            /// <param name="zipPath">压缩文件路径</param>
            /// <param name="filePath">指定文件或文件夹路径</param>
            /// <param name="savePath">解压后存放路径</param>
            /// <param name="isFloder">是否是文件夹 1 是 0 否</param>
            /// <param name="isHasDirectory">当是指定文件夹时是否保存文件夹 1 是 0否</param>
            /// <param name="fileNames">不需解压的文件名多个文件名以|分割 没有传空</param>
            public void UnZipedFile(string zipPath,string filePath,string savePath,string isFloder,string isHasDirectory,string fileNames)
            {
                switch (isFloder)
                {
                    case"1":
                        SpecifyFolder(zipPath, filePath, savePath, isHasDirectory,fileNames);
                        break;
                    case"2":
                        SpecifyFile(zipPath, filePath, savePath,fileNames);
                        break;
                    default:
                        break;
                }
            }
    
            /// <summary>
            /// 压缩文件中是否存在指定文件
            /// </summary>
            /// <param name="zipPath">压缩文件路径</param>
            /// <param name="filePath">指定文件路径</param>
            /// <returns>是否存在 true 是 false 否</returns>
            public bool IsHasFile(string zipPath, string filePath)
            {
                FileStream fileStream = File.Open(zipPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                bool isHasFile = false;
                //获取源文件的长度 
                long length = fileStream.Length;
                if (length <= 0)
                    return isHasFile; 
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream); 
                ZipEntry theEntry; 
                while ((theEntry = s.GetNextEntry()) != null)
                { 
                    _fileName = theEntry.Name;
                    string zipEntryName = Path.GetFileName(theEntry.Name);
                    string mFileName = Path.GetFileName(filePath);
                    string mDirectoryName = Path.GetDirectoryName(filePath);
                    string zipDirectoryName = zipPath + "\\";
                    if (!theEntry.Name.Equals(@"/"))
                    {
                        zipDirectoryName += Path.GetDirectoryName(theEntry.Name).Replace(@"\", "\\");
                    }
                    //不是这个文件夹的同名文件不能解压
                    if (mFileName.Equals(zipEntryName) && mDirectoryName.Equals(mDirectoryName))
                    {
    
                        try
                        {
    
                            if (zipEntryName != String.Empty)
                            { 
                                isHasFile=true; 
                            }
                        }
                        catch (Exception e)
                        {
                            isHasFile = false;
                            //Log.Writer.WriteLog(ex);
                        }
                        break;
                    }
    
                }
                fileStream.Close();
                s.Close();
                return isHasFile; 
            }
    
            /// <summary>
            /// 解压制定文件
            /// </summary>
            /// <param name="zipFilename">压缩文件路径</param>
            /// <param name="filename">指定文件路径</param>
            /// <param name="savePath">解压后存放路径</param>
            ///<param name="fileNames">不需解压的文件名多个文件名以|分割 没有传空</param>
            public void SpecifyFile(string zipPath, string filePath, string savePath,string fileNames)
            {
                FileStream fileStream = File.Open(zipPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                //获取源文件的长度
                long length = fileStream.Length;
                if (length <= 0)
                    return;
    
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream);
    
                ZipEntry theEntry;
                int readlength = 0;
                while ((theEntry = s.GetNextEntry()) != null)
                {
    
                    _fileName = theEntry.Name;
                    string zipEntryName = Path.GetFileName(theEntry.Name);
                    string mFileName = Path.GetFileName(filePath);
                    string mDirectoryName = Path.GetDirectoryName(filePath);
                    string zipDirectoryName = zipPath + "\\";
                    if (!theEntry.Name.Equals(@"/"))
                    {
                        zipDirectoryName += Path.GetDirectoryName(theEntry.Name).Replace(@"\", "\\");
                    }
                    //不是这个文件夹的同名文件不能解压
                    if (mFileName.Equals(zipEntryName) && mDirectoryName.Equals(mDirectoryName))
                    {
    
                        try
                        {
    
                            if (zipEntryName != String.Empty)
                            {
                                if (!fileNames.Contains(zipEntryName))
                                {
                                    //解压文件到指定的目录 
                                    string saveFilePath = System.IO.Path.Combine(savePath, zipEntryName);
                                    if (System.IO.File.Exists(saveFilePath))
                                    {
                                        DeleteOldFile dOldFile = new DeleteOldFile();
                                        dOldFile.RenameOldFile(saveFilePath);
                                    }
                                    FileStream streamWriter = File.Create(System.IO.Path.Combine(savePath, zipEntryName));
                                    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);
                                            readlength += size;
                                            if (readlength >= length)
                                            {
                                                readlength = (int)length;
                                            }
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    streamWriter.Flush();
                                    streamWriter.Close();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                            //Log.Writer.WriteLog(ex);
                        }
                        break;
                    }
    
                }  
                fileStream.Close();
                s.Close();
            }
    
            /// <summary>
            /// 解压制定文件夹
            /// </summary>
            /// <param name="zipFilename">压缩文件路径</param>
            /// <param name="filename">指定文件夹路径</param>
            /// <param name="savePath">解压后存放路径</param>
            /// <param name="isHasDirectory">当是指定文件夹时是否保存文件夹 1 是 0否</param>
            /// <param name="fileNames">不需解压的文件名多个文件名以|分割 没有传空</param>
            public void SpecifyFolder(string zipFilename, string filename, string savePath,string isHasDirectory,string fileNames)
            {
                FileStream fileStream = File.Open(zipFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                //获取源文件的长度
                long length = fileStream.Length;
                if (length <= 0)
                    return;
    
                //获取读取流
                ZipInputStream s = new ZipInputStream(fileStream);
    
                ZipEntry theEntry;
                int readlength = 0;
                while ((theEntry = s.GetNextEntry()) != null)
                {
    
                    _fileName = theEntry.Name;
                    string zipEntryName = Path.GetFileName(theEntry.Name);
                    string mFileName = Path.GetFileName(filename);
                    string zipDirectoryName = zipFilename + "\\";
                    if (!theEntry.Name.Equals(@"/"))
                    {
                        zipDirectoryName += Path.GetDirectoryName(theEntry.Name).Replace(@"\", "\\");
                    }
                    if (zipDirectoryName.Contains(filename))//非此文件夹内的文件不能解压缩
                    {
    
                        try
                        {
                            string mDirectoryName = System.IO.Path.Combine(savePath, Path.GetDirectoryName(theEntry.Name));
                            //是否只要文件 1 是
                            if (isHasDirectory.Equals("1"))
                            { 
                                if (!Directory.Exists(mDirectoryName))
                                {
                                    Directory.CreateDirectory(mDirectoryName);
                                }
                            }
                            if (zipEntryName != String.Empty)
                            {
                                if (!fileNames.Contains(zipEntryName))
                                {
                                    //解压文件到指定的目录 
                                    string saveFilePath = System.IO.Path.Combine(savePath, zipEntryName);
                                    if (System.IO.File.Exists(saveFilePath))
                                    {
                                         
                                        DeleteOldFile dOldFile = new DeleteOldFile();
                                        dOldFile.RenameOldFile(saveFilePath);
                                    }
                                    FileStream streamWriter = File.Create(System.IO.Path.Combine(savePath, zipEntryName));
                                    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);
                                            readlength += size;
                                            if (readlength >= length)
                                            {
                                                readlength = (int)length;
                                            }
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    streamWriter.Flush();
                                    streamWriter.Close();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            fileStream.Close();
                            s.Close();
                            throw ex;
                            //Log.Writer.WriteLog(ex);
                        }
    
                    }
    
                }//end while
    
                //s.Flush();
                fileStream.Close();
                s.Close();
            }
        }
    
     
  • 相关阅读:
    百度浏览器 h5页面fixed布局不显示问题
    Vue3_arco.design icon 组件批量渲染
    异步加载数据的 js tree (基于Jquery)
    ASP.NET MVC 与 WebForm 对比
    asp.net 页面生命周期
    ASP.NET通过OLE DB操作Excel
    SQL 分页查询的几种方式
    OLEDB读取Excel文件丢失部分数据
    MVC 基于FormsAuthentication 方式的权限验证
    Session不超时
  • 原文地址:https://www.cnblogs.com/chengjunwei/p/2557625.html
Copyright © 2020-2023  润新知