问题描述
- 怎么用C#语言在不引用DLL文件的情况下压解ZIP格式文件
-
跪求高手指点,求指点大概思路,现在感觉什么思路都没有,不知道从那里下手
解决方案
利用C#反射技术动态调用dll方法,无需引用
解决方案二:
看看这个类行不行
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace XCInternetExplorer
{
/// <summary>
/// 压缩文件
/// </summary>
public class CreateZip
{
#region 压缩解压单一文件
/// <summary>
/// 压缩单一文件
/// </summary>
/// <param name="sourceFile">源文件路径</param>
/// <param name="destinationFile">目标文件路径</param>
public static void CompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}
public static void CompressFile(byte[] source, string destinationFile)
{
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(source, 0, source.Length);
}
}
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="sourceFile">源文件路径</param>
/// <param name="destinationFile">目标文件路径</param>
public static void DecompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
{
byte[] quartetBuffer = new byte[4];
int position = (int)sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read(quartetBuffer, 0, 4);
sourceStream.Position = 0;
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
byte[] buffer = new byte[checkLength + 100];
using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
{
int total = 0;
for (int offset = 0; ; )
{
int bytesRead = decompressedStream.Read(buffer, offset, 100);
if (bytesRead == 0) break;
offset += bytesRead;
total += bytesRead;
}
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
{
destinationStream.Write(buffer, 0, total);
destinationStream.Flush();
}
}
}
}
#endregion
#region 压缩解压文件夹
/**/
/// <summary>
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
/// </summary>
/// <param name="dirPath">目标文件夹</param>
/// <param name="fileName">压缩文件</param>
public static void CompressFolder(string dirPath, string fileName)
{
ArrayList list = new ArrayList();
foreach (string f in Directory.GetFiles(dirPath))
{
byte[] destBuffer = File.ReadAllBytes(f);
SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer);
list.Add(sfi);
}
IFormatter formatter = new BinaryFormatter();
using (Stream s = new MemoryStream())
{
formatter.Serialize(s, list);
s.Position = 0;
CreateCompressFile(s, fileName);
}
}
/**/
/// <summary>
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
/// </summary>
/// <param name="fileName">压缩文件</param>
/// <param name="dirPath">解压缩目录</param>
public static void DeCompressFolder(string fileName, string dirPath)
{
using (Stream source = File.OpenRead(fileName))
{
using (Stream destination = new MemoryStream())
{
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
{
byte[] bytes = new byte[4096];
int n;
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
{
destination.Write(bytes, 0, n);
}
}
destination.Flush();
destination.Position = 0;
DeSerializeFiles(destination, dirPath);
}
}
}
private static void DeSerializeFiles(Stream s, string dirPath)
{
BinaryFormatter b = new BinaryFormatter();
ArrayList list = (ArrayList)b.Deserialize(s);
foreach (SerializeFileInfo f in list)
{
string newName = dirPath + Path.GetFileName(f.FileName);
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
{
fs.Write(f.FileBuffer, 0, f.FileBuffer.Length);
fs.Close();
}
}
}
private static void CreateCompressFile(Stream source, string destinationName)
{
using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
{
using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
{
byte[] bytes = new byte[4096];
int n;
while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
{
output.Write(bytes, 0, n);
}
}
}
}
#endregion
}
//测试
//private void btnZip_Click(object sender, EventArgs e)
//{
// Common.Zip.CompressFile("database.mdb", "test.zip");
// Common.Zip.CompressFolder("sourcefolder", "sourcefolder.zip");
//}
//private void btnUnZip_Click(object sender, EventArgs e)
//{
// Common.Zip.DecompressFile("test.zip", "database2.mdb");
// Common.Zip.DeCompressFolder("sourcefolder.zip", "folder/");
//}
[Serializable]
class SerializeFileInfo
{
public SerializeFileInfo(string name, byte[] buffer)
{
fileName = name;
fileBuffer = buffer;
}
string fileName;
public string FileName
{
get
{
return fileName;
}
}
byte[] fileBuffer;
public byte[] FileBuffer
{
get
{
return fileBuffer;
}
}
}
}
时间: 2024-09-08 22:26:33