如何创建Web API控制器,生成并返回从内存中JPEG文件(MemoryStream对象)集合流式传输的压缩zip文件.我正在尝试使用DotNetZip库.我找到了这个例子:http://www.4guysfromrolla.com/articles/092910-1.aspx#postadlink.但是,Response.OutputStream在Web API中不可用,因此该技术不能正常工作.因此我尝试将zip文件保存到新的MemoryStream中; 但它扔了.最后,我尝试使用PushStreamContent.这是我的代码:
public HttpResponseMessage Get(string imageIDsList) {
var imageIDs = imageIDsList.Split(',').Select(_ => int.Parse(_));
var any = _dataContext.DeepZoomImages.Select(_ => _.ImageID).Where(_ => imageIDs.Contains(_)).Any();
if (!any) {
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
var dzImages = _dataContext.DeepZoomImages.Where(_ => imageIDs.Contains(_.ImageID));
using (var zipFile = new ZipFile()) {
foreach (var dzImage in dzImages) {
var bitmap = GetFullSizeBitmap(dzImage);
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Jpeg);
var fileName = string.Format("{0}.jpg", dzImage.ImageName);
zipFile.AddEntry(fileName, memoryStream);
}
var response = new HttpResponseMessage(HttpStatusCode.OK);
var memStream …Run Code Online (Sandbox Code Playgroud) 试图将文件提取到给定的文件夹,忽略zipfile中的路径,但似乎没有办法.
考虑到其中实现的所有其他好东西,这似乎是一个相当基本的要求.
我错过了什么?
代码是 -
using (Ionic.Zip.ZipFile zf = Ionic.Zip.ZipFile.Read(zipPath))
{
zf.ExtractAll(appPath);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将zip文件上传到服务器使用C# (Framework 4),以下是我的代码.
string ftpUrl = ConfigurationManager.AppSettings["ftpAddress"];
string ftpUsername = ConfigurationManager.AppSettings["ftpUsername"];
string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl + "Transactions.zip");
request.Proxy = new WebProxy(); //-----The requested FTP command is not supported when using HTTP proxy.
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
StreamReader sourceStream = new StreamReader(fileToBeUploaded);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", …Run Code Online (Sandbox Code Playgroud) 在Windows中解压缩文件时,我偶尔会遇到路径问题
使用DotNetZip时,ZipFile.Read(path)每当阅读带有这些问题之一的zip文件时,调用都会被废弃.这意味着我甚至无法尝试过滤掉它.
using (ZipFile zip = ZipFile.Read(path))
{
...
}
Run Code Online (Sandbox Code Playgroud)
处理阅读这些文件的最佳方法是什么?
更新:
来自此处的示例拉链:https: //github.com/MonoReports/MonoReports/zipball/master
重复:https: //github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cs https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DatasourceType的.cs
以下是有关异常的更多详细信息:
Ionic.Zip.ZipException:无法读取它作为ZipFile
---> System.ArgumentException:已添加具有相同键的>项目. System.ChrowArgumentException
(ExceptionResource资源)
at System.Collections.Generic.Dictionary 2.Add(TKey key,TValue value) at Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile zf) at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile) ZF)2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary
解析度:
根据@ Cheeso的建议,我可以从流中读取所有内容,避免重复内容和路径问题:
//using (ZipFile zip = ZipFile.Read(path))
using (ZipInputStream stream = new ZipInputStream(path))
{
ZipEntry e;
while( (e = stream.GetNextEntry()) != null )
//foreach( ZipEntry e in zip)
{ …Run Code Online (Sandbox Code Playgroud) 我使用DotNetZip将文件从一个文件添加MemoryStream到一个zip文件,然后将该zip文件保存为一个,MemoryStream以便我可以将其作为附件发送.下面的代码没有错误,但MemoryStream必须做得不对,因为它是不可读的.当我将拉链保存到我的硬盘驱动器时,一切都很完美,只是当我尝试将其保存到流中时.
using (ZipFile zip = new ZipFile())
{
var memStream = new MemoryStream();
var streamWriter = new StreamWriter(memStream);
streamWriter.WriteLine(stringContent);
streamWriter.Flush();
memStream.Seek(0, SeekOrigin.Begin);
ZipEntry e = zip.AddEntry("test.txt", memStream);
e.Password = "123456!";
e.Encryption = EncryptionAlgorithm.WinZipAes256;
var ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);
zip.Save(ms);
//ms is what I want to use to send as an attachment in an email
}
Run Code Online (Sandbox Code Playgroud) 我在文件夹中创建了一个文本文件并压缩了该文件夹并保存了@same位置以供测试.我想在创建后直接在用户计算机上下载该zip文件.我正在使用dotnetzip库并完成以下操作:
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + "sample.zip");
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
zip.Save(Server.MapPath("~/Directories/hello/sample.zip"));
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议如何在用户端下载zip文件.
Ionic zip允许我将现有文件添加到zip对象并创建一个zip文件.但考虑到我正在从创建的zip文件中读取那些byte []并通过服务器发送,我需要再次从该字节[]创建zip文件以在服务器上存储zip.我该如何实现这一目标?
我正在使用C#.
我使用的是最新版本的离子拉链版本1.9.1.8.我已经设置了离子拉链的属性 ParallelDeflateThreshold = 0.在过去的两个月里,压缩机制工作得很好.突然间,这停止了工作.压缩线程只是挂起,离子zip只是创建了tmp文件而无法创建zip文件.即使文件很小,我也可以轻松地重现这个问题.
我对这个问题的分析如下
问题在于最新版本的离子拉链,在这种情况下,离子拉链在创建zip文件时被挂起.我们注意到,使用此dll的其他几个用户也在其网站中报告了此类错误.请参考链接.这个问题将通过禁用ParallelThreshold离子zip的属性来解决,但它会延迟大型日志文件的性能,因为它可以在单线程而不是多线程模式下工作.
现在通过将ParallelDeflateThreshold属性设置为默认值来解决问题.但是我找不到这个问题的确切原因.为什么拉链失败突然?没有机器更换.
我正在尝试在内存中压缩和解压缩数据(因此,我不能使用FileSystem),并且在下面的示例中,当数据被解压缩时,它在我原始数据的末尾有一种填充('\ 0'字符) .
我究竟做错了什么 ?
[Test]
public void Zip_and_Unzip_from_memory_buffer() {
byte[] originalData = Encoding.UTF8.GetBytes("My string");
byte[] zipped;
using (MemoryStream stream = new MemoryStream()) {
using (ZipFile zip = new ZipFile()) {
//zip.CompressionMethod = CompressionMethod.BZip2;
//zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
zip.AddEntry("data", originalData);
zip.Save(stream);
zipped = stream.GetBuffer();
}
}
Assert.AreEqual(256, zipped.Length); // Just to show that the zip has 256 bytes which match with the length unzipped below
byte[] unzippedData;
using (MemoryStream mem = new MemoryStream(zipped)) {
using (ZipFile unzip = ZipFile.Read(mem)) {
//ZipEntry …Run Code Online (Sandbox Code Playgroud) 我想验证像 Zip 这样的多部分压缩文件,因为当压缩文件缺少任何部分时,它会引发错误,但我想在提取之前验证它,不同的软件会创建不同的命名结构。
我还提到了一个与DotNetZip相关的问题。
下面的截图来自 7z 软件。
第二个屏幕截图来自 C# 的 DotNetZip。
还有一件事是我还想测试它是否也已损坏或不像 7z 软件。请参阅下面的屏幕截图了解我的要求。
请帮我解决这些问题。