最近要做一个文件交互,上传和下载, 都是zip压缩文件,所以研究了下,写了如下的示例

注意引用  ICSharpCode.SharpZipLib.dll 文件

该dll文件可以到官方网站去下载, 我这里提供一个 .Net FrameWork 2.0 版本的 ,点此下载

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip; namespace ConZIPTools
{
class Program
{
public static void Main(string[] args)
{
try
{
// ZipTool.UnZIP(@"F:\SharpZipLib_0860_Bin.zip", @"F:\M\SharpZipLib_0860_Bin\"); ZipTool.CreateZIP(@"F:\M\SharpZipLib_0860_Bin\net-11", @"F:\m.zip");
}
catch (Exception ex)
{ Console.WriteLine(ex.Message);
}
Console.WriteLine("程序执行完毕");
Console.ReadLine();
}
} public class ZipTool
{
/// <summary>
/// zip 压缩文件, 解压
/// </summary>
/// <param name="zipFilePath">zip压缩文件路径, 例如;F:\SharpZipLib_0860_Bin.zip</param>
/// <param name="saveDirectory">压缩文件加压后的路径,例如 F:\M\SharpZipLib_0860_Bin\ ,注意后面要带'\'</param>
public static void UnZIP(string zipFilePath, string saveDirectory)
{
// Perform simple parameter checking.
if (!File.Exists(zipFilePath))
{
Console.WriteLine("指定的文件路径找不到:" + zipFilePath);
return;
}
if (!Directory.Exists(saveDirectory))
{
//解压后存放的 文件夹路径
Directory.CreateDirectory(saveDirectory);
} using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{ ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
Console.WriteLine("解压出来的文件名:" + theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name); // create directory
if (directoryName.Length > )
{
//创建目录
string saveDir = saveDirectory + directoryName;
Directory.CreateDirectory(saveDir);
} if (fileName != String.Empty)
{
string saveFilePath = saveDirectory + theEntry.Name;
using (FileStream streamWriter = File.Create(saveFilePath))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
} } /// <summary>
/// 创建 zip压缩文件
/// </summary>
/// <param name="fileDirectoryPath">需要压缩的文件夹路径,注意只压缩里面的文件, 不包括该文件夹下的子文件夹</param>
/// <param name="saveZipFile">保存后zip文件路径</param>
public static void CreateZIP(string fileDirectoryPath, string saveZipFile)
{
// Perform some simple parameter checking. More could be done
// like checking the target file name is ok, disk space, and lots
// of other things, but for a demo this covers some obvious traps. if (!Directory.Exists(fileDirectoryPath))
{
Console.WriteLine("Cannot find directory '{0}'", fileDirectoryPath);
return;
} try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(fileDirectoryPath); if (filenames.Length<)
{
Console.WriteLine("该路径下的没有文件:" + fileDirectoryPath);
return;
}
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(saveZipFile)))
{
s.SetLevel(); // 0 - store only to 9 - means best compression byte[] buffer = new byte[]; foreach (string file in filenames)
{ // Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
var entry = new ZipEntry(Path.GetFileName(file)); // Setup the entry data as required. // Crc and size are handled by the library for seakable streams
// so no need to do them here. // Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file))
{ // Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
} // Finish/Close arent needed strictly as the using statement does this automatically // Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish(); // Close is important to wrap things up and unlock the file.
s.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex); // No need to rethrow the exception as for our purposes its handled.
}
}
}
}

最新文章

  1. 《CLR.via.C#第三版》第二部分第8,9章节读书笔记(四)
  2. appium踩过的坑(2):java.lang.NoSuchFieldError:INSTANCE
  3. CSS3之绽放的花朵(网页效果--每日一更)
  4. Android Studio 简单设置
  5. 有四中方法可以实现PHP的伪静态,你造吗?
  6. Sublime text 3 中文文件名显示方框怎么解决?
  7. 查看SqlServer的内存使用情况
  8. js 参数的 引用与值传递
  9. 模仿ios下的coverflow
  10. jquery创始人
  11. EditPlus配置C环境
  12. 对一个表中所有列数据模糊查询adoquery
  13. 子窗口url调整导致父窗口刷新
  14. HDU 6040---Hints of sd0061(STL)
  15. 201521123048 《Java程序设计》第3周学习总结
  16. 解决Sublime text 3 中文文件名显示方框
  17. 使用cookie来做身份认证
  18. Vue-admin工作整理(一):项目搭建
  19. EM 算法最好的解释
  20. [工作积累] Tricks with UE4 PerInstanceRandom

热门文章

  1. CF1101A Minimum Integer 模拟
  2. Spring Boot中JPA如何实现按日期合计
  3. 1.搭建JavaEE开发环境
  4. Vue循环中多个input绑定指定v-model
  5. vue 的watch用法
  6. MyCnblog Style
  7. Linux下Tomcat启动关闭命令
  8. Dropping Balls UVA - 679(二叉树的遍历)
  9. 性能测试工具LoadRunner09-LR之Virtual User Generator 日志
  10. qt安装