上传文件压缩后没有后缀名

爱捉弄你 2009-10-13 06:19:27
上传文件压缩后没有后缀名

public static void CompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}

适用上面的代码 上传文件 到制定的地方 后解压开 没有后缀名

是代码本身的问题么......



....大家有更好的代码么 有的话分享下....谢谢啦....



....小弟在线等
...全文
992 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengrenyi 2012-03-15
  • 打赏
  • 举报
回复
这么多年了,不知道LZ还在线不。。。今天被同样的问题困扰。于是我用了以下代码解决:
private void dabaoduogewenjian()
{
// 压缩
ZipOutputStream s = new ZipOutputStream(File.Create("D:\\bbbb" + ".zip"));
//生成一个GZipOutputStream流,用来生成压缩文件。
//因为GZipOutputStream由Stream派生,所以它可以赋给Stream。
FileStream fs = File.OpenRead("D:\\bbbb.doc");
FileStream fs1 = File.OpenRead("D:\\data.xml");
//生成一个文件流,它用来打开要压缩的文件
//可以使用System.IO.File的静态函数OpenRead来生成文件流
byte[] writeData = new byte[fs.Length];
//指定缓冲区的大小
fs.Read(writeData, 0, (int)fs.Length);
//读入文件
ZipEntry entry = new ZipEntry("bbbb.doc");
s.PutNextEntry(entry);
s.Write(writeData, 0, writeData.Length);

writeData =new byte[fs1.Length];
fs1.Read(writeData, 0, (int)fs1.Length);
ZipEntry entry1 = new ZipEntry("data.xml");
s.PutNextEntry(entry1);
s.Write(writeData, 0, writeData.Length);

//写入压缩文件
s.Close();
//关闭文件
}
fable说 2009-10-13
  • 打赏
  • 举报
回复
不懂,请高人解决……
woaixiayutian 2009-10-13
  • 打赏
  • 举报
回复
难道是所上传的服务器有问题?或许是服务器启用了“隐藏已知文件类型的扩展名”。。。
wuyq11 2009-10-13
  • 打赏
  • 举报
回复
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}

System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}

System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;
Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;

static bool GZipFile(string sourcefilename, string zipfilename)
{
bool blResult;
FileStream srcFile = File.OpenRead(sourcefilename);
GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipfilename,FileMode.Create));
try
{
byte[] FileData = new byte[srcFile.Length];
srcFile.Read(FileData, 0, (int)srcFile.Length);
zipFile.Write(FileData, 0, FileData.Length);
blResult = true;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
blResult = false;
}
srcFile.Close();
zipFile.Close();
return blResult;
}




zhengliyan_star 2009-10-13
  • 打赏
  • 举报
回复
下班了,帮顶了!
PandaIT 2009-10-13
  • 打赏
  • 举报
回复
帮顶看下有星星没!!

62,243

社区成员

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

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

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

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