使用 System.IO.Packaging 在 C# 中创建 zip
我正在尝试在服务器上动态创建 zip 文件,其中包含来自在线服务的照片。但是当我尝试打开该文件时,WinRar 说该文件格式未知或已损坏。
这是代码:
MemoryStream stream = new MemoryStream();
Package package = ZipPackage.Open(stream, FileMode.Create, FileAccess.ReadWrite);
foreach (Order o in orders)
{
o.GetImages();
Parallel.ForEach(o.Images, (Dictionary<string, object> image) =>
{
string imageId = (string)image["ID"];
int orderId = (int)image["OrderId"];
string fileName = (string)image["FileName"];
string url = (string)image["URL"];
if (string.IsNullOrEmpty(fileName))
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData(url);
MemoryStream ms = new MemoryStream(data);
ms.Write(data, 0, data.Length);
string ext;
switch(wc.ResponseHeaders["Content-Type"])
{
case "image/jpeg":
case "image/jpg":
ext = "jpg";
break;
case "image/png":
ext = "png";
break;
case "image/gif":
ext = "gif";
break;
default :
ext = "un";
break;
}
Uri uri = new Uri("/" + orderId.ToString() + "/" + imageId + "." + ext, UriKind.Relative);
var part = package.CreatePart(uri, wc.ResponseHeaders["Content-Type"], CompressionOption.NotCompressed);
var pstream = part.GetStream(FileMode.Open, FileAccess.ReadWrite);
pstream.Write(data, 0, data.Length);
var rel = package.CreateRelationship(part.Uri, TargetMode.Internal, "http://example.com/AlbumImage");
}
});
}
package.Flush();
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, Convert.ToInt32(stream.Length));
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=Orders.zip");
Response.AddHeader("Content-Length", stream.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(fileBytes);
Response.End();
I am trying to create zip file dynamically on the server that includes photos from online services. But when I try to open the file, WinRar sais the the file is unknown format or damaged.
this is the code:
MemoryStream stream = new MemoryStream();
Package package = ZipPackage.Open(stream, FileMode.Create, FileAccess.ReadWrite);
foreach (Order o in orders)
{
o.GetImages();
Parallel.ForEach(o.Images, (Dictionary<string, object> image) =>
{
string imageId = (string)image["ID"];
int orderId = (int)image["OrderId"];
string fileName = (string)image["FileName"];
string url = (string)image["URL"];
if (string.IsNullOrEmpty(fileName))
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData(url);
MemoryStream ms = new MemoryStream(data);
ms.Write(data, 0, data.Length);
string ext;
switch(wc.ResponseHeaders["Content-Type"])
{
case "image/jpeg":
case "image/jpg":
ext = "jpg";
break;
case "image/png":
ext = "png";
break;
case "image/gif":
ext = "gif";
break;
default :
ext = "un";
break;
}
Uri uri = new Uri("/" + orderId.ToString() + "/" + imageId + "." + ext, UriKind.Relative);
var part = package.CreatePart(uri, wc.ResponseHeaders["Content-Type"], CompressionOption.NotCompressed);
var pstream = part.GetStream(FileMode.Open, FileAccess.ReadWrite);
pstream.Write(data, 0, data.Length);
var rel = package.CreateRelationship(part.Uri, TargetMode.Internal, "http://example.com/AlbumImage");
}
});
}
package.Flush();
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, Convert.ToInt32(stream.Length));
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=Orders.zip");
Response.AddHeader("Content-Length", stream.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(fileBytes);
Response.End();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这都是关于帕克·乌里斯的。我们也被他们打过。这是在 zip 中存储两个文件的工作示例。代码刚刚从我们的项目复制,并删除了一些私人数据。
It's all about Pack Uris. We also have been beaten by them. Here is working sample for storing two files in zip. Code just copied from our project with removing some private data.
我知道这个问题有点老了,但我也遇到过类似的问题。我用过
并且有效
I know that this question is a bit old but I had a similar problem. I used
and it worked