想用c#做一个压缩和解压缩的工具。
请问还需要知道哪些方面的知识呢?
要对汇编有了解不?
请问还需要知道哪些方面的知识呢?
要对汇编有了解不?
解决方案 »
- c#如何对webbrowser框架中的复选框执行选中操作?
- gridview导出数据为pdf格式
- 如何通过传入参数类型判断调用的类的方法!!!!!!!!!!!
- 用户在text框点击后,得到用户的输入焦点??
- ???谁有比较好的进销存源代码
- 怎样获得当前代码所在的文件的名称和行号?
- 关于 ADO.NET 的异步更新, 删除失败, 无任何相应的 Constrain , 请指教
- 有网站可以在线采集视频数据,我感到很奇怪,他是怎么做到的???
- 看书上说:“使用【C#字段向导】对话框为窗体Form1添加一个字段”,要问得是【C#字段向导】从哪可以调出来?
- 正则表达式
- 数据查询的行数
- C# datagridview(或C1grid)的问题,都来看看
从网站上随便找一个算法,就实现了。
最著名的是7Z算法,连RAR和Zip都给你解压了,
现成的算法、现成的代码。
下载回来吓一跳,framework是被rar压缩过的
下载回来吓一跳,framework是被rar压缩过的
SHARPZIP C#下的类库,可实现解、压缩!
b:2
c:2
d:3
z:2
14
0/ \1
6 \
0/\1 \
4 \ 8
0/\1 \ 0/\1
b2 c2 z2 d3 a5 a:11
b:000
c:001
d:10
z:01abdcabdaaazzcd
11000100011100010111111010100110
=
C4717EA6
{
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="filenames">文件名列表,若是文件夹以“\”结尾</param>
/// <param name="GzipFileName">压缩文件名</param>
/// <param name="CompressionLevel">压缩比</param>
/// <param name="password">解压密码</param>
public static void DoZip(List<string> filenames, string GzipFileName, int CompressionLevel,string password)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
if (!string.IsNullOrEmpty(password))
{
s.Password = password;
}
try
{
s.SetLevel(CompressionLevel);
foreach (string filename in filenames)
{
//若是文件夹
if (filename.EndsWith("\\"))
{
doZipFolder(string.Format("{0}\\", Directory.GetParent(filename)), filename, ref s);
}
//若是文件
else
{
FileInfo file = new FileInfo(filename);
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ continue; } byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime); s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length); fs.Close();
}
}
}
finally
{
s.Finish();
s.Close();
}
} //递归压缩文件夹中的文件并保持文件目录结构
private static void doZipFolder(string basePath, string fullPath, ref ZipOutputStream s)
{
DirectoryInfo dir = new DirectoryInfo(fullPath);
//压缩文件
foreach (FileInfo file in dir.GetFiles())
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{
continue;
}
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file.FullName.Replace(basePath, ""));
entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
fs.Close();
}
//递归压缩子文件夹
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
doZipFolder(basePath, subDir.FullName, ref s);
}
} /// <summary>
/// 解压缩文件
/// </summary>
/// <param name="GzipFile">Zip文件路径</param>
/// <param name="dPath">解压路径</param>
/// <param name="password">解压密码</param>
public static void UnZip(string GzipFile, string dPath,string password)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile));
if (!string.IsNullOrEmpty(password))
{
s.Password = password;
}
string directoryName = Path.GetDirectoryName(dPath + "\\") + "\\";
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);
string CurrentDirectory = directoryName;
byte[] data = new byte[2048];
int size = 2048;
ZipEntry theEntry = null;
FileStream streamWriter = null;
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{
if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
else
{
if (theEntry.Name != String.Empty)
{
string fullName = CurrentDirectory + theEntry.Name;
string folder = fullName.Substring(0, fullName.LastIndexOf("\\"));
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
streamWriter = File.Create(CurrentDirectory + theEntry.Name);
while (true)
{
size = s.Read(data, 0, data.Length);
if (size <= 0) break; streamWriter.Write(data, 0, size);
}
streamWriter.Close();
}
}
}
s.Close();
}
}
对于rar和7z貌似可用在C#中调命令行方式完成