通过程序压缩/解压文件

通过引用一DLL(ICSharpCode.dll)可以实现所述功能。。。

一、压缩文件

using System;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;

namespace wsUpFiles
{
 /// <summary>
 /// Common 的摘要说明。
 /// </summary>
 public class Common
 {
  public Common()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  /// <summary>
  /// 压缩文件
  /// </summary>
  /// <param name="sourceFileNames">压缩文件名称集合</param>
  /// <param name="destFileName">压缩后文件名称</param>
  /// <param name="password">密码</param>
  public static void zipFile(string path,string destFileName)
  {
   Crc32 crc = new Crc32();
   ZipOutputStream s = new ZipOutputStream(File.Create(destFileName));
   s.Password="";
   s.SetLevel(6); // 0 - store only to 9 - means best compression
   //定义
   System.IO.DirectoryInfo myDir = new DirectoryInfo(path);
   if(myDir.Exists == true)
   {
    System.IO.FileInfo[] myFileAry = myDir.GetFiles();
    
    //循环提取文件夹下每一个文件,提取信息,
    foreach (FileInfo objFiles in myFileAry)
    {
     FileStream fs = File.OpenRead(objFiles.FullName);
     byte[] buffer = new byte[fs.Length];
     fs.Read(buffer, 0, buffer.Length);
     ZipEntry entry = new ZipEntry(objFiles.FullName);
     entry.DateTime = DateTime.Now;
     entry.Size = fs.Length;
     fs.Close();
     crc.Reset();
     crc.Update(buffer);
     entry.Crc  = crc.Value;
     s.PutNextEntry(entry);
     s.Write(buffer, 0, buffer.Length);           
    }   
    s.Finish();
    s.Close();
   }
  }
  
 }
}

要对压缩文件加密时,要s.Password = "aaa"; aaa为密码。

二、解压文件

 /// <summary>
  /// 解压文件
  /// </summary>
  /// <param name="sourceFileName">被解压文件名称</param>
  /// <param name="destPath">解压后文件目录</param>
  /// <param name="password">密码</param>
  public static void unzipFile(string sourceFileName,string destPath,string fileType)
  {
   ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFileName));
   ZipEntry theEntry;
   ArrayList al=new ArrayList();

   while ((theEntry = s.GetNextEntry()) != null)
   {   
    string fileName=Path.GetFileName(theEntry.Name);
    if(fileName!="")
    {
     fileName=destPath+"//"+fileName;
     if(!Directory.Exists(destPath))
     {
      Directory.CreateDirectory(destPath);
     }
     FileStream streamWriter = File.Create(fileName);     
     int size = 2048;
     byte[] data = new byte[2048];
     s.Password="";
     while (true)
     {
      size = s.Read(data, 0, data.Length);
      if (size > 0)
      {
       streamWriter.Write(data, 0, size);
      }
      else
      {
       break;
      }
     }
     streamWriter.Close();
    }

   }
   s.Close();
   
  }

注意:程序的压缩过的文件,要通过系统上的工具解压出来的路径会相当多,因其在压缩时保留了原来的绝对路径,但压缩的文件中只包含所压缩的目标文件,当用程序解压出来的文件是相对的文件路径。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值