用下面的代码压缩,可以压缩但里面同时保留目录结构,一打开压缩包就看到h:点进去看到imagesys是文件名再点进去download再点进去看到GUID文件名再点进去真正看到图片了,够晕了要点这么多次再能看到想要的图片,原来图片的路径随机生成在h:\imagesys\download\guid\2008.jpeg
现在的问题是有没有办法直接把2008.jpeg压缩进去呢,路径就不要压了/// <summary>
/// 压缩目录
/// </summary>
/// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>
public static void ZipFileDictory(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]); 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();
}
现在的问题是有没有办法直接把2008.jpeg压缩进去呢,路径就不要压了/// <summary>
/// 压缩目录
/// </summary>
/// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>
public static void ZipFileDictory(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]); 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();
}
解决方案 »
- windows服务在windows server 2008上运行的问题
- VS项目自动生成bin目录问题
- 一机连多个串口设备,给个解决方案!
- 模拟鼠标问题
- 关于TcpServerChannel和TcpChannel的区别是什么?
- C#调用VC的Dll时遇到的IntPtr类型转化成byte[]的问题
- 鼠标悬浮在饼图的不同位置,显示不同的提示信息.怎么实现?
- DataGridView自定义combox数据列取值问题
- 急死我了,大家帮帮忙,谢谢!如何在.net环境下做打印控制??谢谢!
- 请教各位如何言简意赅的描述一下几个概念,以及他们的使用上的心得,在线
- 用 C# + mssql 开发winform多年,现有人要我开发一个网站
- c#中comboBox控件默认值
/// 压缩目录
/// </summary>
/// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名) </param>
public static void ZipFileDictory(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]); 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); int i = file.LastIndexOf(@"\");
string f;
if (i >= 0)
{
f = file.Substring(i + 1);
}
else
{
f=file ;
}
ZipEntry entry = new ZipEntry(f); 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();
}
{
string[] filenames = Directory.GetFiles(args[0]); 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(Path.GetFileName(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();
}改称这样就可以了
可以用第三方的控件我却要自己写.....SharpZipLib.dll
不能用于商业用途...
public static void ZipFileDictory(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]); 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(Path.GetFileName(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();
} 改称这样就可以了