//请求接口
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);
//接收返回结果
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
Stream stream = fileResp.GetResponseStream();
System.IO.BinaryReader streamReader = new BinaryReader(stream, System.Text.Encoding.GetEncoding("UTF-8"));
//确认文件本地保存位置
string rarUrl = HttpContext.Current.Server.MapPath("~/Storage/master");
string zipName = rarUrl + "/Orders" + dateStr + ".zip";
if (System.IO.File.Exists(zipName))
{
System.IO.File.Delete(zipName);
}
FileInfo file = new System.IO.FileInfo(zipName);
using (file.Create()){}
var l = fileResp.ContentLength.ToInt_();
FileStream fs = file.OpenWrite();
BinaryWriter fos = new BinaryWriter(fs);
var end = streamReader.ReadBytes(l);
fos.Write(end, 0, l);
fos.Flush();
fos.Close();
fs.Close();
streamReader.Close();//关闭流
读取压缩文件内的文件内容
public ActionResult ExportICBCOrdersToExcel(string zip)
{
var zipUrl = Server.MapPath("~/Storage/master/ICBC/" + zip);
if (string.IsNullOrWhiteSpace(zip) || !System.IO.File.Exists(zipUrl))
{
throw new HimallException("错误的对账单信息");
}
var zipName = zip.Substring(0, zip.IndexOf("."));
var _binPath = Server.MapPath("~/Storage/master/ICBC/" + zipName);
if (System.IO.File.Exists(zipName))
{
System.IO.File.Delete(zipName);
}
//获取html字符串
var html = UnZip(zipUrl, _binPath);
//移除占位符
var array = html.Replace("rn", "t").Split('t');
//订单总数
var count = array[0].Replace("u001b", "");
//转换成models
var models = ArrayToModel(array);
return ExcelView("ExportICBCAccountStatement", "对账单信息", models);
}
///
/// 解压功能(解压压缩文件到指定目录)
///
/// 待解压的文件
/// 指定解压目标目录
/// 解压结果
public static string UnZip(string fileToUnZip, string zipedFolder)
{
var html = "";
//解压文件
PrepareDataFiles(zipedFolder, fileToUnZip);
//读取文件内容
DirectoryInfo dir = new DirectoryInfo(zipedFolder);
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
FileInfo fi = (FileInfo)fsi;
FileStream fs = fi.OpenRead();
BinaryReader br = new BinaryReader(fs);
var len = fi.Length.ToInt();
var bs = br.ReadBytes(len);
br.Close();
fs.Close();
html = Encoding.UTF8.GetString(bs);
}
}
return html;
}
///
/// 解压zip
///
/// 解压文件夹路径
/// 待解压的zip路径
private static void PrepareDataFiles(string dir, string filename)
{
using (ZipFile zip = ZipFile.Read(Path.Combine(filename)))
{
foreach (ZipEntry file in zip)
{
file.Extract(dir, ExtractExistingFileAction.OverwriteSilently);
}
}
}
public static List ArrayToModel(string[] array)
{
List list = new List();
//第一个是订单总条数
for (var i = 1; i < array.Length; i++)
{
var s = array[i].Split('u001b');
if (string.IsNullOrWhiteSpace(array[i]) || s.Length < 45)
{
continue;
}
var model = new Model();
model.id= s[0];
model.name= s[1];
list.Add(model);
}
return list;
}



