网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

实现在服务器端在线解压.ZIP文件【附源码下载】

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

之前用过一个用PHP 写的一个可以在服务器端在线解压.ZIP文件的小工具(只有一个文件),但最近,公司项目是用.NET 开发的,服务器上不支持PHP ,所以就自己动手写了个。【源码下载

解压的核心代码是从网上找的。不知道是哪位大侠写的。小的我就拿来用了。用了ICSharpCode.SharpZipLib.dll类库。

为了让文件的个数减少到最少,所有的后台代码都写到前台aspx页面中了。

  1. <%@ Page Language="C#" %> 
  2. <%@ Import Namespace="System.Collections.Generic" %> 
  3. <%@ Import Namespace="System.Web" %> 
  4. <%@ Import Namespace="System.Web.UI"%> 
  5. <%@ Import Namespace="System.Web.UI.WebControls" %> 
  6. <%@ Import Namespace="System.IO" %> 
  7. <%@ Import Namespace="System.Threading"%> 
  8. <%@ Import Namespace="ICSharpCode.SharpZipLib.Zip" %> 
  9.  
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  11.  
  12. <html xmlns="http://www.w3.org/1999/xhtml"> 
  13. <head runat="server"> 
  14.     <title></title> 
  15.     <script runat="server" type="text/C#"> 
  16.         protected void Page_Load(object sender, EventArgs e) 
  17.         { 
  18.             if (!IsPostBack) 
  19.             { 
  20.                 string f = Server.MapPath("."); 
  21.                 foreach (FileInfo fi in new System.IO.DirectoryInfo(f).GetFiles()) 
  22.                 { 
  23.                     if (fi.Name.LastIndexOf(".zip") > 0) 
  24.                     { 
  25.                         ddlFileList.Items.Add(new ListItem(fi.Name, fi.Name)); 
  26.                     } 
  27.                 } 
  28.                 ddlFileList.Items.Insert(0, new ListItem("please select", "-1")); 
  29.  
  30.             } 
  31.  
  32.         } 
  33.  
  34.         public void btnDecompress_Click(object s, EventArgs e) 
  35.         { 
  36.              
  37.             string zipFilePath = Server.MapPath("./") + ddlFileList.SelectedValue.Trim();// 
  38.             if (File.Exists(zipFilePath)) 
  39.             { 
  40.                 string TargetPath = txtTargetPath.Text.Trim(); 
  41.                 if (TargetPath.LastIndexOf("\\") < 0
  42.                 { 
  43.                     TargetPath += "\\\\"; 
  44.                 } 
  45.                 string targetPath = Server.MapPath(TargetPath); 
  46.                 FileCompression.Decompress(zipFilePath, targetPath); 
  47.                 Response.Write("文件解压成功,请在" + TargetPath + "文件夹查看。"); 
  48.             } 
  49.             else 
  50.             { 
  51.                 Response.Write("压缩文件不存在,请先压缩文件。"); 
  52.             } 
  53.         } 
  54.  
  55.         public class FileCompression 
  56.         { 
  57.             /// <summary> 
  58.             /// 构造函数 
  59.             /// </summary> 
  60.             public FileCompression() 
  61.             { 
  62.             } 
  63.  
  64.             #region 加密、压缩文件 
  65.  
  66.             /// <summary> 
  67.             /// 压缩文件 
  68.             /// </summary> 
  69.             /// <param name="fileNames">要打包的文件列表</param> 
  70.             /// <param name="GzipFileName">目标文件名</param> 
  71.             /// <param name="CompressionLevel">压缩品质级别(0~9)</param> 
  72.             /// <param name="SleepTimer">休眠时间(单位毫秒)</param>      
  73.             public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer) 
  74.             { 
  75.                 ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName)); 
  76.                 try 
  77.                 { 
  78.                     s.SetLevel(CompressionLevel);   //0 - store only to 9 - means best compression 
  79.  
  80.                     foreach (FileInfo file in fileNames) 
  81.                     { 
  82.                         FileStream fs = null
  83.                         try 
  84.                         { 
  85.                             fs = file.Open(FileMode.Open, FileAccess.ReadWrite); 
  86.                         } 
  87.                         catch 
  88.                         { continue; } 
  89.  
  90.                         //  方法二,将文件分批读入缓冲区 
  91.                         byte data = new byte[2048]; 
  92.                         int size = 2048
  93.                         ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name)); 
  94.                         entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime); 
  95.  
  96.                         s.PutNextEntry(entry); 
  97.  
  98.                         while (true) 
  99.                         { 
  100.                             size = fs.Read(data, 0, size); 
  101.                             if (size <= 0) break; 
  102.  
  103.                             s.Write(data, 0, size); 
  104.                         } 
  105.  
  106.                         fs.Close(); 
  107.                         file.Delete(); 
  108.  
  109.                         Thread.Sleep(SleepTimer); 
  110.                     } 
  111.                 } 
  112.                 finally 
  113.                 { 
  114.                     s.Finish(); 
  115.                     s.Close(); 
  116.                 } 
  117.             } 
  118.  
  119.             #endregion 
  120.  
  121.             #region 解密、解压缩文件 
  122.  
  123.             /// <summary> 
  124.             /// 解压缩文件 
  125.             /// </summary> 
  126.             /// <param name="GzipFile">压缩包文件名</param> 
  127.             /// <param name="targetPath">解压缩目标路径</param>        
  128.             public static void Decompress(string GzipFile, string targetPath) 
  129.             { 
  130.                 //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\"; 
  131.                 string directoryName = targetPath
  132.                 if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录 
  133.                 string CurrentDirectory = directoryName
  134.  
  135.                 byte data = new byte[2048]; 
  136.                 int size = 2048
  137.  
  138.                 ZipEntry theEntry = null
  139.                 using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile))) 
  140.                 { 
  141.                     while ((theEntry = s.GetNextEntry()) != null) 
  142.                     { 
  143.                         if (theEntry.IsDirectory) 
  144.                         {// 该结点是目录 
  145.                             if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name); 
  146.                         } 
  147.                         else 
  148.                         { 
  149.                             if (theEntry.Name != String.Empty) 
  150.                             { 
  151.                                 //解压文件到指定的目录 
  152.                                 using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name)) 
  153.                                 { 
  154.                                     while (true) 
  155.                                     { 
  156.                                         ssize = s.Read(data, 0, data.Length); 
  157.                                         if (size <= 0) break; 
  158.  
  159.                                         streamWriter.Write(data, 0, size); 
  160.                                     } 
  161.                                     streamWriter.Close(); 
  162.                                 } 
  163.                             } 
  164.                         } 
  165.                     } 
  166.                     s.Close(); 
  167.                 } 
  168.             } 
  169.  
  170.             #endregion 
  171.  
  172.         } 
  173.              
  174.          
  175.      
  176.     </script> 
  177.      
  178.      
  179.      
  180. </head> 
  181. <body> 
  182.     <form id="form1" runat="server"> 
  183.     <div> 
  184.     选择文件<asp:DropDownList ID="ddlFileList" runat="server"></asp:DropDownList><br /> 
  185.     目标文件夹:<asp:TextBox ID="txtTargetPath" runat="server"></asp:TextBox><br /> 
  186.      
  187.         <asp:Button ID="btnDecompress" runat="server"  
  188.             Text="解压缩" onclick="btnDecompress_Click" /> 
  189.      
  190.     </div> 
  191.     </form> 
  192. </body> 
  193. </html> 
设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师