zip file 压缩文件

更新 2019-03-26

从 upload file stream 直接做成 zip file

[HttpPost("upload")]
public async Task<ActionResult<string>> Upload([FromForm] UploadFileData data)
{
using (var fileStream = data.file.OpenReadStream())
{
using (var zip = ZipFile.Open($@"C:\keatkeat\my-projects\aspnetcore-projects\asp.net core 2.2\zip-file\Project\dada.zip", ZipArchiveMode.Create))
{
var entry = zip.CreateEntry("abc.pdf");
using (var entryStream = entry.Open())
{
await fileStream.CopyToAsync(entryStream);
}
}
}
return Ok();
}

有时候我们希望 upload 文件后自动压缩, 可以节省空间.

可以使用微软提供的压缩代码

Install-Package System.IO.Compression.ZipFile -Version 4.3.0

refer :

http://www.cnblogs.com/haogj/archive/2012/11/17/2774733.html

http://www.cnblogs.com/skyivben/archive/2012/03/09/2388482.html

string root = AppDomain.CurrentDomain.BaseDirectory;
string[] inputFiles = new string[] { root + "angular1.min.js", root + "Capture.PNG" };
string outputFile = root + "myZip4.zip"; FileInfo file = new FileInfo(outputFile);
if (file.Exists) throw new Exception("error");
bool notExist = inputFiles.Any(f =>
{
FileInfo fileInfo = new FileInfo(f);
return !fileInfo.Exists;
});
if (notExist) throw new Exception("error"); using (var zip = ZipFile.Open(outputFile, ZipArchiveMode.Create))
{
foreach (var inputFile in inputFiles)
{
string fileName = Path.GetFileName(inputFile);
zip.CreateEntryFromFile(inputFile, fileName);
}
} //ZipFile.CreateFromDirectory(root + "myfolder", root + "abc.zip");

可以在上传文件保持后进行操作

[ODataRoute("uploadFile")]
public async Task<IHttpActionResult> uploadFile()
{
if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
string root = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles";
var provider = new Helper.MyMultipartFormDataStreamProvider(root);
await Request.Content.ReadAsMultipartAsync(provider);
string localFilePath = provider.FileData[].LocalFileName;
//do zip..., delete original file, if multiple then loop
string fileName = Path.GetFileNameWithoutExtension(localFilePath) + ".zip";
return Ok(fileName);
}

另一种情况可以是在下载的时候选择性压缩

前端可以使用 <a download href= "/download?path=a.jpg,b.jpg" >

href 可以通过 mvvm 工具做成动态.

尽量不要使用 Blob 之类的,不然依赖太多.

后台通过 path params zip 了文件使用 return FIleResult 返回前端就可以了.

如果文件不大的话,可以使用缓存保存 zip stream 然后返回 stream

string root = AppDomain.CurrentDomain.BaseDirectory;
var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var demoFile = archive.CreateEntryFromFile(root + "angular.min.js", "angular.min.js");
}
memoryStream.Seek(, SeekOrigin.Begin);
string filepath = AppDomain.CurrentDomain.BaseDirectory + "angular.min.zip";
string contentType = MimeMapping.GetMimeMapping(filepath);
return File(memoryStream, contentType, "angular.min.zip");

注意

1.memoryStream 不要用 using , FileResult 会负责处理.

2.new ZipArchive 第3个参数 leaveOpen 要 true, 这样 ZipArchive displose 时, memoryStream 还可以使用.

3.memoryStream.Seek 必须把 posotion 调回 0 哦.

上一篇:How do I create a zip file?(转)


下一篇:maven时候Embedded error: error in opening zip file