我用GZipStream压缩文件夹后,发现原来文件夹中的文件扩展名没了,怎么办啊????这是代码: 这是文件夹路径 string path = Server.MapPath("../../../UpLoads/Attachment/" + attachment);
这是压缩后文件路径 string fileName =path + ".rar";
Compress(path, fileName);
public void Compress(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);
}
} private 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);
}
}
}
} [Serializable]
public 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;
}
} }
这是压缩后文件路径 string fileName =path + ".rar";
Compress(path, fileName);
public void Compress(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);
}
} private 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);
}
}
}
} [Serializable]
public 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;
}
} }
解决方案 »
- 如何解决这个问题
- 求一个任何客户端(PC)均可行的身份验证方法
- 怎么来判断这个验证码是否是和文本框相同呢?
- 问个类似javascript就可以实现的问题,大哥们帮帮忙
- 在VS2005中,我在一个解决方案中添加了一个类库,但有个问题,详情请进!
- 在记录各用户的状态时,除了用SESSION,还有没有别的方法?
- 求4.3或4.4的jmail专业版,能用来收pop3邮件的组件~~~~~~~
- checkboxlist问题
- 我用 http://xxxxxxx/cx.aspx?id=12345 传一个变量,用 request("id")接收变量,这样可以?但是?
- 回家了真高兴(散分)
- asp.net网站主页不在网站根目录下面?
- 在线求时间正则表达时
一股脑地把 byte[] 堆成一堆去了?!改变你的 list 类型设计,让它保存文件名。
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
using (FileStream outfile = File.Create(Path.ChangeExtension(filepath, "zip")))
{
using (GZipStream zipStream = new GZipStream(outfile, CompressionMode.Compress))
{
zipStream.Write(bytes, 0, bytes.Length);
}
}
}
还可使用SharpZipLib解压缩
private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
s.PutNextEntry(entry);
s.Flush();
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + 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);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}
folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
} return res;
}
private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
{
bool res;
if (!Directory.Exists(FolderToZip))
{
return false;
} ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.SetLevel(6);
s.Password = Password; res = ZipFileDictory(FolderToZip, s, ""); s.Finish();
s.Close(); return res;
}
private static bool ZipFile(string FileToZip, string ZipedFile, String Password)
{
if (!File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("压缩的文件: " + FileToZip + " 不存在!");
}
FileStream ZipFile = null;
ZipOutputStream ZipStream = null;
ZipEntry ZipEntry = null; bool res = true;
try
{
ZipFile = File.OpenRead(FileToZip);
byte[] buffer = new byte[ZipFile.Length];
ZipFile.Read(buffer, 0, buffer.Length);
ZipFile.Close(); ZipFile = File.Create(ZipedFile);
ZipStream = new ZipOutputStream(ZipFile);
ZipStream.Password = Password;
ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6); ZipStream.Write(buffer, 0, buffer.Length);
}
catch
{
res = false;
}
finally
{
if (ZipEntry != null)
{
ZipEntry = null;
}
if (ZipStream != null)
{
ZipStream.Finish();
ZipStream.Close();
}
if (ZipFile != null)
{
ZipFile.Close();
ZipFile = null;
}
GC.Collect();
GC.Collect(1);
}
return res;
}
参考