zoukankan      html  css  js  c++  java
  • .Net使用SharpZip解压缩文件

    最近,项目中使用到了上传压缩文件,文件上传到服务器后,肯定要解压,取出其中的文件才能使用,在这里做一个小结,Get这个新技能。

    首先在使用NuGet管理程序在项目中添加引用ICSharpCode.SharpZipLib.dll.

    在项目公共文件夹中添加SharpZip类。

     1 using ICSharpCode.SharpZipLib.Checksums;
     2 using ICSharpCode.SharpZipLib.Zip;
     3 using Microsoft.Win32;
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Diagnostics;
     7 using System.IO;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Threading.Tasks;
    11 
    12 namespace App.Common.SharpZip
    13 {
    14 
    15     public class SharpZip
    16     {
    17         public SharpZip() { }
    18 
    19         /// <summary>  
    20         /// 解压缩  
    21         /// </summary>  
    22         /// <param name="file">待解压文件名(包含物理路径)</param>  
    23         /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param>  
    24         public static bool UnpackFiles(string file, string dir)
    25         {
    26             try
    27             {
    28                 if (!Directory.Exists(dir))
    29                 {
    30                     Directory.CreateDirectory(dir);
    31                 }
    32                 ZipInputStream s = new ZipInputStream(File.OpenRead(file));
    33                 ZipEntry theEntry;
    34                 while ((theEntry = s.GetNextEntry()) != null)
    35                 {
    36                     string directoryName = Path.GetDirectoryName(theEntry.Name);
    37                     string fileName = Path.GetFileName(theEntry.Name);
    38                     if (directoryName != String.Empty)
    39                     {
    40                         Directory.CreateDirectory(dir + directoryName);
    41                     }
    42                     if (fileName != String.Empty)
    43                     {
    44                         FileStream streamWriter = File.Create(dir + fileName);
    45                         int size = 2048;
    46                         byte[] data = new byte[2048];
    47                         while (true)
    48                         {
    49                             size = s.Read(data, 0, data.Length);
    50                             if (size > 0)
    51                             {
    52                                 streamWriter.Write(data, 0, size);
    53                             }
    54                             else
    55                             {
    56                                 break;
    57                             }
    58                         }
    59                         streamWriter.Close();
    60                     }
    61                 }
    62                 s.Close();
    63                 return true;
    64             }
    65             catch (Exception)
    66             {
    67                 throw;
    68             }
    69         }
    70     }
    71 }
    View Code

    接着就可以方便的使用这个新技能了

     1 /// <summary>
     2         /// 解压缩
     3         /// </summary>
     4         /// <returns></returns>
     5         public ActionResult UnPack()
     6         {
     7             ///.....
     8             ///
     9 
    10 
    11             var fullPath = Request.MapPath("/File/Temp/test.zip");
    12             var unpackPath = Request.MapPath("/File/Temp/");
    13             var unpackResult = SharpZip.UnpackFiles(fullPath, unpackPath);
    14             if (unpackResult)
    15             {
    16                 ///......
    17                 ///
    18             }
    19 
    20 
    21             ///.....
    22             ///
    23             return Content("success");
    24         }
    View Code
  • 相关阅读:
    已知: 每个飞机只有一个油箱, 飞机之间可以相互加油(注意是相互,没有加油机) 一箱油可供一架飞机绕地球飞半圈,问题:为使至少一架飞机绕地球一圈回到起飞时的飞机
    简易vector的实现
    简单的内存池实现
    归并排序,递归与非递归
    堆排序
    位运算
    二叉树的建立,以及非递归遍历
    “云端融合”思想的自我摸索(很不靠谱)
    linux android开发环境搭建
    Android系统架构及内核简介
  • 原文地址:https://www.cnblogs.com/wenha/p/10325171.html
Copyright © 2011-2022 走看看