asp.net如何实现将文件压缩,在线等

cg20 2008-11-19 03:35:36
我这样写的代码存为txt文件,可是点击链接下载此文件的时候是在窗口中打开,而不是另存为。没办法了,那在代码里如何才能将txt再进行压缩保存呢?

System.IO.StreamWriter sr;
fileName = Server.MapPath("..\\readbook\\download\\" + ds.Tables[0].Rows[0]["b_name"] + ".txt");
if (!File.Exists(fileName))
{
sr = File.AppendText(fileName);
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sr.Write(ds.Tables[0].Rows[i]["b_name1"].ToString() + "<br/><br/>" + ds.Tables[0].Rows[i]["b_name2"].ToString() + "<br/><br/>" + Server.HtmlDecode(Encoding.Unicode.GetString((byte[])ds.Tables[0].Rows[i]["b_message"])) + "<br/><br/>");
}
sr.Flush();
sr.Close();
}
...全文
214 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
liumangdaheng 2008-11-20
  • 打赏
  • 举报
回复
#region 压缩文件夹
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="ZipFromFileDictory">文件路径</param>
public void ZipMultiFile(string ZipFromFileDictory)
{
string[] strFiles = Directory.GetFiles(ZipFromFileDictory);
string strZipFileName = ZipFromFileDictory + ".zip";

if (File.Exists(strZipFileName))
{
File.Delete(strZipFileName);
}
string name = strZipFileName.Substring(strZipFileName.LastIndexOf("\\") + 1);
if (name == ".zip")
{
Response.Write("<script>alert('没有数据')</script>");
}
else
{
//需要压缩文件的个数
string[] strFilePaths = new string[strFiles.Length];

MemoryStream oMemoryStream = new MemoryStream();

ZipOutputStream oZipStream = new ZipOutputStream(File.Create(strZipFileName));

for (int i = 0; i <= strFiles.Length - 1; i++)
{
FileStream oReadFileStream = File.OpenRead(strFiles[i]);
byte[] btFile = new byte[oReadFileStream.Length];
oReadFileStream.Read(btFile, 0, btFile.Length);


string strCurrentFileName = Path.GetFileName(strFiles[i]);
strFilePaths[i] = ZipFromFileDictory + "/" + strCurrentFileName;

ZipEntry oZipEntry = new ZipEntry(strCurrentFileName);

oZipEntry.DateTime = DateTime.Now;
oZipEntry.Size = oReadFileStream.Length;

Crc32 oCrc32 = new Crc32();
oCrc32.Reset();
oCrc32.Update(btFile);


oZipEntry.Crc = oCrc32.Value;

oZipStream.PutNextEntry(oZipEntry);
oZipStream.Write(btFile, 0, btFile.Length);

oReadFileStream.Close();
}
oZipStream.Finish();
oZipStream.Close();

System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "Online; filename=" + name);
System.Web.HttpContext.Current.Response.ContentType = "text/plain";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.WriteFile(strZipFileName);
System.Web.HttpContext.Current.Response.End();

}

}
#endregion
lqscoke 2008-11-19
  • 打赏
  • 举报
回复


//压缩
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
the_Info = " a " + " 1.rar " + " " + @"C:\1\1.txt";
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\1";//获取或设置要启动的进程的初始目录。
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("压缩成功");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
Fibona 2008-11-19
  • 打赏
  • 举报
回复
通过引用 ICSharpCode.SharpZipLib.dll

或者用

using java.util;
using java.util.zip;这两个类来实现,这是J++代码,Jdk1.1中的类

直接引用vjslib就行

bolome 2008-11-19
  • 打赏
  • 举报
回复
关注
evjen 2008-11-19
  • 打赏
  • 举报
回复

using System.IO;
using System.IO.Compression;

public static void fileCompress(string f_name)
{
string filename =HttpContext.Current.Server.MapPath(f_name);
FileStream infile = File.OpenRead(filename);
byte[] buffer = new byte[infile.Length];
infile.Read(buffer, 0, buffer.Length);
infile.Close();

FileStream outfile = File.Create(Path.ChangeExtension(filename, "zip"));
GZipStream gz = new GZipStream(outfile, CompressionMode.Compress);
gz.Write(buffer, 0, buffer.Length);
gz.Close();
}


压缩的
xbfitliu 2008-11-19
  • 打赏
  • 举报
回复
你可以把txt文件,读入到流中,然后压缩流。
这样好实现点。
takako_mu 2008-11-19
  • 打赏
  • 举报
回复
不會,友情UP.
「已注销」 2008-11-19
  • 打赏
  • 举报
回复
up
cg20 2008-11-19
  • 打赏
  • 举报
回复
不要解压,要的是压缩
Zhanlixin 2008-11-19
  • 打赏
  • 举报
回复
我这里有一段解压缩的原代码:
private string UnZipFile(string strFile)
{
try
{
int i = strFile.LastIndexOf("\\");
string strFileName = strFile.Substring(i + 1);
string strFileName1 = strFileName.Substring(0, strFileName.LastIndexOf("."));

//string strPath = strFile.Substring(0,strFile.Length- strFile.LastIndexOf(".")+1);
// string strPath = strFile.Substring(0, strFile.Length - i + 1);
string strPath = strFile.Substring(0, i);
strPath = strPath.Replace("-", "");

string strPathFile = strPath + "\\" + strFileName1;



//string ServerDir = Server.MapPath("");//rar路径
System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
Process1.StartInfo.FileName = ServerDir + "\\Rar.exe";
Directory.CreateDirectory(strPathFile); //创建解压文件夹

//if (!File.Exists(strFile))
//{
// Response.Write("<script language='javascript'>alert('" + strFile + "不存在');</script>");

//}

Process1.StartInfo.Arguments = " e " + strFile + " " + strPathFile + " -y";


Process1.Start();//解压开始
while (!Process1.HasExited) //等待解压的完成
{
}
string strBak = strPath + "\\" + strFileName1 + "\\tjnb" + strNd + ".dat";
string strReport = strPath + "\\" + strFileName1 + "\\上报.dat";
if (File.Exists(strBak))
{
return strBak;
}
else if (File.Exists(strReport))
{
return strReport;
}
else
{
return "";
}

//File.Delete(strPath + "\\" + strFileName + ".rar");//删除rar文件
}
catch (Exception ex)
{
throw ex;
return "";

}
}
beyondjay 2008-11-19
  • 打赏
  • 举报
回复
ICSharpCode.SharpZipLib.dll

62,041

社区成员

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

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

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

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