简体   繁体   中英

Download multiple Mp3 files from different urls as zip archive in asp.Net

I am working on a .Net Application in which i need to add multiple mp3 files to a zip archive and download the zip archive locally. The mp3 files are on different urls ad are not hosted or stored on my server. Which library is good for such thing. I tried using SharpLipZip but failed. Here is my code which i am currently trying to use with sharpziplib. All the code is executed but browser doesnt download.

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();

The definition of ReadStream is as follows.

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();
    }
}

Well, thats the same thing which I am also building for my website, anyhow i was trying to look for the zip file structure first to create the zip file manually instead of using any other library. Until now, i am only able to get the structure of the zip file :

https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html

Here, it mentions that you need to have the CRC32 of the file first which is being appended to the zip file, so thats the tricky part in my side. Let me know once you gets any updates for the same.

Good Luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM