我正在开发一个.Net应用程序,我需要将多个mp3文件添加到zip存档并在本地下载zip存档。 mp3文件位于不同的网址上广告未托管或存储在我的服务器上。哪个库对这样的东西有好处。我尝试使用SharpLipZip但失败了。这是我目前正在尝试使用sharpziplib的代码。所有代码都已执行,但浏览器无法下载。
string[] fileURLs = new string[] { "http://www.musicimpressions.com/demos_mp3g/d_RE41843.mp3", "http://media.archambault.ca/sample/6/2/B/0/62B0CC2D91D4357D6477845E967AF9BA/00000000000000027923-256K_44S_2C_cbr1x_clipped.mp3" };
Response.AddHeader("Content-Disposition", "attachment; filename=CallRecordings.zip");
Response.ContentType = "application/x-zip-compressed";
ZipOutputStream zipStream = new ZipOutputStream(Response.OutputStream);
zipStream.SetLevel(3);
byte[] buffer = new byte[10485760];
foreach (string url in fileURLs)
{
Stream fileStream = null;
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;
//Get the Stream returned from the response
fileStream = fileResp.GetResponseStream();
byte[] fileBytes = ReadStream(fileStream);
ZipEntry fileEntry = new ZipEntry(ZipEntry.CleanName(url));
fileEntry.Size = fileBytes.Length;
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
Response.Flush();
fileStream.Close();
}
zipStream.Finish();
zipStream.Flush();
zipStream.Close();
Response.Flush();
Response.End();
ReadStream的定义如下。
public static byte[] ReadStream(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
答案 0 :(得分:1)
嗯,这也是我为我的网站构建的相同的东西,无论如何我试图首先寻找zip文件结构来手动创建zip文件而不是使用任何其他库。到目前为止,我只能得到zip文件的结构:
https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html
在这里,它提到你需要首先将文件的CRC32附加到zip文件中,这就是我身边的棘手部分。一旦获得相同的更新,请告诉我。
祝你好运:)