谁用ICSharpCode制作过压缩解压文件夹的程序,帮忙看看
我用ICSharpCode制作的压缩解压程序 现在压缩没有问题 可以解压出线了问题在winform中又正常
附上源码看看:
下面是压缩的程序
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
/// <summary>
/// ZipFloClass 的摘要说明
/// </summary>
public class ZipFloClass
{
public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
//如果文件没有找到,则报错
if (!(FileToZip))
{
throw new ("指定要压缩的文件: " + FileToZip + " 不存在!");
}
StreamToZip = new (FileToZip, , );
ZipFile = (ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
public static string filePath = "";
public static string GetContrast(string path)
{
string[] objFiles = Directory.GetFiles(path);
for (int i = 0; i < objFiles.Length; i++)
{
if (filePath.Equals(""))
filePath = objFiles[i];
else
filePath += "<IS1>" + objFiles[i];
}
string[] objDorectories = Directory.GetDirectories(path);
for (int i = 0; i < objDorectories.Length; i++)
{
GetContrast(objDorectories[i]);
}
return filePath;
}
/// <summary>
/// 压缩目录
/// </summary>
/// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>
public static void ZipFileDictory(string[] args)
{
string[] filenames = GetContrast(args[0]).Split(new string[] { "<IS1>" }, StringSplitOptions.None);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
s.SetLevel(6);
foreach (string file in filenames)
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
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();
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名</param>
public static void ZipFile(string FileToZip, string ZipedFile)
{
//如果文件没有找到,则报错
if (!File.Exists(FileToZip))
{
throw new ("指定要压缩的文件: " + FileToZip + " 不存在!");
}
FileStream fs = File.OpenRead(FileToZip);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
FileStream ZipFile = File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
ZipStream.Write(buffer, 0, buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
}
下面是解压的程序:
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
/// <summary>
/// UnZipFloClass 的摘要说明
/// </summary>
public class UnZipFloClass
{
/// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="args">待解压的文件</param>
/// <param name="args">指定解压目标目录</param>
public static void UnZip(string[] args)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));
ZipEntry theEntry;
//if (!Directory.Exists(@args[1].Trim()))
//{
// Directory.CreateDirectory(@args[1].Trim());
//}
string path = @args[1].Trim();
string rootDir = " ";
string rootFile = " ";
while ((theEntry = s.GetNextEntry()) != null)
{
//string directoryName = Path.GetDirectoryName(theEntry.Name);
rootDir = Path.GetDirectoryName(theEntry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= 0)
{
rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
}
string dir = Path.GetDirectoryName(theEntry.Name);
//根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name);
//根目录下的文件名称
if (dir != " ")
//创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(@args[1].Trim() + dir))
{
path = @args[1].Trim() + dir;
//在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == " " && fileName != "")
//根目录下的文件
{
path = @args[1].Trim();
rootFile = fileName;
}
else if (dir != " " && fileName != "")
//根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf("\\") > 0)
//指定文件保存的路径
{
path = @args[1].Trim() + dir;
}
}
if (dir == rootDir)
//判断是不是需要保存在根目录下的文件
{
path = @args[1].Trim() + rootDir;
}
//Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\" + fileName);
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);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
}
在解压的时候读入文件数据流时出错 :
size = s.Read(data, 0, data.Length); 就是这句
还有while ((theEntry = s.GetNextEntry()) != null)这句在循环第二次也出错
调用方法是
压缩:
string[] FilePathS = new string[2];
FilePathS[0] = "需要压缩目录";
FilePathS[1] = "压缩后存放地址加上压缩文件名字";
ZipFloClass.ZipFileDictory(FilePathS);
调用解压 :
string[] FilePathS = new string[2];
FilePathS[0] = "需要解压文件";
FilePathS[1] = "文件解压存放地址";
UnZipFloClass.UnZip(FilePathS);
SharpZipLib.dll在附件中
程序为web程序
ICSharpCode.SharpZipLib.rar
(69.69 KB)