【问题标题】:Download multiple Mp3 files from different urls as zip archive in asp.Net在 asp.Net 中从不同的 url 下载多个 Mp3 文件作为 zip 存档
【发布时间】:2013-03-25 13:45:39
【问题描述】:

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

【问题讨论】:

  • .NET 4.5 自带ZipArchive class
  • 以及如何从 url 添加 mp3 文件...任何帮助将不胜感激
  • 我建议你看一下FTP类。 MSDN 上的示例为您提供了一个很好的开始,但请记住,MSDN 示例目前仅适用于文本文档,但将其更新为内存流(或文件流)并不难!

标签: c# asp.net sharpziplib


【解决方案1】:

嗯,这也是我正在为我的网站构建的东西,无论如何我试图首先寻找 zip 文件结构以手动创建 zip 文件,而不是使用任何其他库。到目前为止,我只能获得 zip 文件的结构:

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

在这里,它提到您需要首先将文件的 CRC32 附加到 zip 文件中,所以这就是我这边的棘手部分。一旦您获得任何更新,请告诉我。

祝你好运 :)

【讨论】:

  • 太好了。如果您可以共享相同的代码,我会很高兴。前几天我也分享了一个类似的问题,可能你可以在上面添加一些cmets。 link
猜你喜欢
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多