如何实现,读取远程文件,用GZIP压缩后保存成文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现,读取远程文件,用GZIP压缩后保存成文件相关的知识,希望对你有一定的参考价值。

如何实现,读取远程文件,用GZIP压缩后保存成文件
以前用 wc.DownloadFile(strUrl, file);下载文件,但我现在想把文件GZIP压缩后再保存
所以我改用

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(strUrl);
GZipStream zipStream = new GZipStream(resstr, CompressionMode.Compress);

具体该怎么写就不会了,主要是不知道该如何将 Stream写入文件。最好可以给我个现成的代码。

/// <summary>
/// 文件下载
/// </summary>
/// <param name="filename">文件名称</param>
public void FileDownLoad(string filename)

//本地文件夹
string baseLocation = Server.MapPath("~/UpFile/");

//文件名称
FileInfo fileInfo = new FileInfo(baseLocation + filename);

//文件存在
if (fileInfo.Exists)

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";

Response.Filter.Close();
Response.WriteFile(fileInfo.FullName);

//下载次数+1
string sql = "update ZYWJ set XZCS=XZCS+1 where WJMC1='" + filename + "'";
int R = DAL.DBHelper.GetScalar(sql);

if (R > 0)

//查询是否已经下载过
string sql3 = "select * from TempZYXZ where HYUserName='" + Session["UserName"].ToString() + "' and ZYWJM='" + filename + "'";
DataTable dt3 = DAL.DBHelper.GetDataSet(sql3);

if (dt3.Rows.Count > 0)

//修改单个下载次数
string sql4 = "update TempZYXZ set XZCS =XZCS+1 where HYUserName='" + Session["UserName"].ToString() + "' and ZYWJM='" + filename + "'";
DAL.DBHelper.GetScalar(sql4);


//没有下载过
else

//积分-10
string sql2 = "update HYUser set HYJF=HYJF-10 where HYLoginName ='" + Session["UserName"].ToString() + "'";
DAL.DBHelper.GetScalar(sql2);

//保存下载的文件
string sql5 = "insert into TempZYXZ (HYUserName,ZYWJM,XZCS) values('" + Session["UserName"].ToString() + "','" + filename + "',1) select Id =@@IDENTITY;";
DAL.DBHelper.GetScalar(sql5);

Response.Flush();
Response.Clear();

Response.End();

else

Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('对不起,服务器正在升级!');window.close();;</script>");


else

Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('对不起,资源文件不存在或已被删除!');window.close();;</script>");

参考技术A var buffer1 = new WebClient().DownloadData("http://zhidao.baidu.com/question/205905738.html#here");

MemoryStream ms = new MemoryStream();
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress);
compressedzipStream.Write(buffer1, 0, buffer1.Length);
compressedzipStream.Close();

var buffer2 = ms.ToArray();

FileStream fs = new FileStream(@"d:\baidu11.zip", FileMode.OpenOrCreate);
fs.Write(buffer2, 0, buffer2.Length);
fs.Close();
参考技术B 分三步
1. 取Stream
2. 读Steram到byte[]
3. 将byte[]保存到文件

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();

byte[] buffer = new Byte[response.ContentLength];
stream.Read(buffer, 0, buffer.Length);
stream.Close();

string path="......";
if (!File.Exists(path))

FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
参考技术C 楼上正解
string filePath="你要存放的文件地址";
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Close();

主要就是实例化一个文件流,然后把数据流写入到那个文件中

以上是关于如何实现,读取远程文件,用GZIP压缩后保存成文件的主要内容,如果未能解决你的问题,请参考以下文章

c# gzip压缩后,解压出来文件不能用了

如何把文件压缩成tar.gz

用gzip压缩后怎么文件大小反而大了

Perl | Perl读取gzip压缩文件

linux下的gzip命令如何运用?

怎样在linux下对目录进行压缩生成gz文件