C# 压缩问题

qq3895 2013-12-10 09:29:47

/// 压缩文件
/// </summary>
/// <param name="sourceFilePath"></param>
/// <param name="destinationZipFilePath"></param>
public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
{
if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
sourceFilePath += System.IO.Path.DirectorySeparatorChar;
}


ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(destinationZipFilePath));
zipStream.SetLevel(6); // 压缩级别 0-9
CreateZipFiles(sourceFilePath, zipStream);
zipStream.Finish();
zipStream.Close();

}

private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream)
{
string[] files = Directory.GetDirectories(sourceFilePath);
for (int i = 0; i < files.Length; i++)
{
string[] file = Directory.GetFiles(files[i].ToString());
Crc32 crc = new Crc32();
foreach (string name in file)
{

FileStream fileStream = System.IO.File.OpenRead(name);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
string filename = Path.GetFileName(name);
ZipEntry entry = new ZipEntry(filename); //filename是xxx.jpg
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}



我运行之后,是xx.zip,图标是一张白纸,
求解!!!!!!!
这是压缩代码
...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yannis_lin 2013-12-11
  • 打赏
  • 举报
回复
#region 下列代码为压缩并下载代码 ZipOutputStream zos = null; String strBaseDir = ""; protected void dlZipDir(string strPath, string strFileName) { MemoryStream ms = null; Response.ContentType = "application/octet-stream"; strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' '); Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip"); ms = new MemoryStream(); zos = new ZipOutputStream(ms); strBaseDir = strPath + "\\"; addZipEntry(strBaseDir); zos.Finish(); zos.Close(); Response.Clear(); Response.BinaryWrite(ms.ToArray()); Response.End(); } protected void addZipEntry(string PathStr) { DirectoryInfo di = new DirectoryInfo(PathStr); foreach (DirectoryInfo item in di.GetDirectories()) { addZipEntry(item.FullName); } foreach (FileInfo item in di.GetFiles()) { FileStream fs = File.OpenRead(item.FullName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string strEntryName = item.FullName.Replace(strBaseDir, ""); ZipEntry entry = new ZipEntry(strEntryName); zos.PutNextEntry(entry); zos.Write(buffer, 0, buffer.Length); fs.Close(); } } #endregion
丨shadow 2013-12-11
  • 打赏
  • 举报
回复
发错了。。。
丨shadow 2013-12-11
  • 打赏
  • 举报
回复
/// <summary> /// 利用 WinRAR 进行解压缩 /// </summary> /// <param name="path">文件解压路径(绝对)</param> /// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param> /// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param> /// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns> internal static bool UnRAR(string path, string rarName) { bool flag = false; string rarexe; RegistryKey regkey; Object regvalue; string cmd; ProcessStartInfo startinfo; Process process; try { regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command"); regvalue = regkey.GetValue(""); rarexe = regvalue.ToString(); regkey.Close(); rarexe = rarexe.Substring(1, rarexe.Length - 7); Directory.CreateDirectory(path); //解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹 cmd = string.Format("x {0} {1} -y", rarName, path); startinfo = new ProcessStartInfo(); startinfo.FileName = rarexe; startinfo.Arguments = cmd; startinfo.WindowStyle = ProcessWindowStyle.Hidden; startinfo.WorkingDirectory = path; process = new Process(); process.StartInfo = startinfo; process.Start(); process.WaitForExit(); if (process.HasExited) { flag = true; } process.Close(); } catch (Exception e) { throw e; } return flag; }
ltfyea 2013-12-10
  • 打赏
  • 举报
回复
#region 压缩单个文件 public void ZipFile(string sFileName,string sFile, string sZipName) { //string[] filenames = Directory.GetFiles(args[0]); Crc32 crc = new Crc32(); ZipOutputStream s = new ZipOutputStream(File.Create(sZipName)); s.SetLevel(6); // 0 - store only to 9 - means best compression //foreach (string file in filenames) { //打开压缩文件 FileStream fs = File.OpenRead(sFileName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(sFile); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. 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(); } #endregion

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧