我正在开发一个 .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();
}
}