[英]Download multiple Mp3 files from different urls as zip archive in asp.Net
我正在.Net應用程序中工作,在該應用程序中,我需要將多個mp3文件添加到zip存檔中,然后在本地下載zip存檔。 mp3文件位於不同的url廣告上,沒有托管或存儲在我的服務器上。 哪個庫適合這種事情。 我嘗試使用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();
}
}
好吧,這就是我也在為我的網站構建的東西,無論如何,我試圖首先尋找zip文件結構來手動創建zip文件,而不是使用任何其他庫。 到目前為止,我只能獲得zip文件的結構:
https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html
在這里,它提到您需要首先擁有該文件的CRC32,並將其附加到zip文件中,因此這是我這一邊的棘手部分。 讓我知道一旦您獲得相同的任何更新。
祝你好運 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.