用下面的代码压缩,可以压缩但里面同时保留目录结构,一打开压缩包就看到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();
}
/// 压缩目录
/// </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();
} 改称这样就可以了