c#下载压缩文件
来源:互联网 发布:侧面导航栏网站源码 编辑:程序博客网 时间:2024/06/03 22:08
//DLL
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
public void DownloadZipFile(string id)
{
string zipFilePath = Property.GetFileUrlString("免冠照片temp", Property.FileEnum.zip);
if (!Directory.Exists(Server.MapPath("~/Template/Template/Zip")))
{
Directory.CreateDirectory(Server.MapPath("~/Template/Template/Zip"));
}
try
{
List<string> filenames = new List<string>();
string filesPath = string.Empty;
foreach (var item in id.Split(','))
{
var result = _pictureAppServices.FirstOrDefault(p => p.Id.ToString() == item);
if (result != null)
{
filesPath = result.Url;
}
filenames.Add(Server.MapPath("~/" + filesPath));
// filenames.Add(); ;
}
//生成的压缩文件为test.zip
using (FileStream fsOut = System.IO.File.Create(Server.MapPath(zipFilePath+".zip")))
{
//ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
foreach (string file in filenames)
{
FileInfo fi = new FileInfo(file);
string entryName = System.IO.Path.GetFileName(file);
//ZipEntry类代表了一个压缩包中的一个项,可以是一个文件,也可以是一个目录。
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
//把压缩项的信息添加到ZipOutputStream中。
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
//把需要压缩文件以文件流的方式复制到ZipOutputStream中。
using (FileStream streamReader = System.IO.File.OpenRead(file))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
//使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
zipStream.IsStreamOwner = false;
zipStream.Finish();
zipStream.Close();
}
}
// filenames = Directory.GetFiles(filesPath);
//using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipFilePath)))
//{
// s.SetLevel(9); // 压缩级别 0-9
// //s.Password = "123"; //Zip压缩文件密码
// byte[] buffer = new byte[4096]; //缓冲区大小
// foreach (string file in filenames)
// {
// ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// entry.DateTime = DateTime.Now;
// s.PutNextEntry(entry);
// using (FileStream fs = System.IO.File.OpenRead(file))
// {
// int sourceBytes;
// do
// {
// sourceBytes = fs.Read(buffer, 0, buffer.Length);
// s.Write(buffer, 0, sourceBytes);
// } while (sourceBytes > 0);
// }
// }
// s.Finish();
// s.Close();
//}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
return File(Server.MapPath(zipFilePath), "application/zip", "免冠照片.zip");
}
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
public void DownloadZipFile(string id)
{
string zipFilePath = Property.GetFileUrlString("免冠照片temp", Property.FileEnum.zip);
if (!Directory.Exists(Server.MapPath("~/Template/Template/Zip")))
{
Directory.CreateDirectory(Server.MapPath("~/Template/Template/Zip"));
}
try
{
List<string> filenames = new List<string>();
string filesPath = string.Empty;
foreach (var item in id.Split(','))
{
var result = _pictureAppServices.FirstOrDefault(p => p.Id.ToString() == item);
if (result != null)
{
filesPath = result.Url;
}
filenames.Add(Server.MapPath("~/" + filesPath));
// filenames.Add(); ;
}
//生成的压缩文件为test.zip
using (FileStream fsOut = System.IO.File.Create(Server.MapPath(zipFilePath+".zip")))
{
//ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
foreach (string file in filenames)
{
FileInfo fi = new FileInfo(file);
string entryName = System.IO.Path.GetFileName(file);
//ZipEntry类代表了一个压缩包中的一个项,可以是一个文件,也可以是一个目录。
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
//把压缩项的信息添加到ZipOutputStream中。
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
//把需要压缩文件以文件流的方式复制到ZipOutputStream中。
using (FileStream streamReader = System.IO.File.OpenRead(file))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
//使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
zipStream.IsStreamOwner = false;
zipStream.Finish();
zipStream.Close();
}
}
// filenames = Directory.GetFiles(filesPath);
//using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipFilePath)))
//{
// s.SetLevel(9); // 压缩级别 0-9
// //s.Password = "123"; //Zip压缩文件密码
// byte[] buffer = new byte[4096]; //缓冲区大小
// foreach (string file in filenames)
// {
// ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// entry.DateTime = DateTime.Now;
// s.PutNextEntry(entry);
// using (FileStream fs = System.IO.File.OpenRead(file))
// {
// int sourceBytes;
// do
// {
// sourceBytes = fs.Read(buffer, 0, buffer.Length);
// s.Write(buffer, 0, sourceBytes);
// } while (sourceBytes > 0);
// }
// }
// s.Finish();
// s.Close();
//}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
return File(Server.MapPath(zipFilePath), "application/zip", "免冠照片.zip");
}
阅读全文
0 0
- c#下载压缩文件
- 压缩文件下载
- C#压缩文件
- C# 压缩文件
- C# 压缩文件
- C# 压缩文件
- c# 压缩文件
- C# 压缩文件
- C# 压缩文件
- C#压缩文件
- C#压缩文件
- c#压缩文件
- C#实现从FTP服务器下载压缩文件并解压.
- 在C#中在服务器上打包压缩文件,并下载
- ZIP压缩文件,并下载
- java下载压缩文件
- SpringMVC下压缩文件下载
- Ubuntu下载压缩文件
- Oracle解决导入CLOB大字段数据失败的问题
- PL/SQL 试题
- Regular polygon HDU
- ECS云服务器[Nginx+PHP+MySQL基本操作命令总结]
- Python函数创建
- c#下载压缩文件
- js页面跳转传参
- Android6.0和7.0新特性分析
- cartographer源码分析(18)-sensor-compressed_point_cloud
- 不要重载&& ||和,操作符
- 设计模式之抽象工厂模式
- linux编译出现/usr/bin/ld: 找不到 -lopencv_dep_cudart 的问题解决
- (N)Telephony分析(二)之SystemServer启动
- HDU6055 Regular polygon【哈希】