大規模なXMLファイルアーカイブをホストするサーバーがあり、APIがzip内の要求されたファイルを取得しています。 11個以下のファイルを選択すると、ジップは正常に戻ってきます。私はより多くを取得する場合は、zipファイルを開こうとすると、私は次のエラーが表示されますファイルをzip形式でダウンロードすると、ASP.NET MVCで破損したzipが発生する
「Windowsは、フォルダを開くことができません圧縮(zip形式)フォルダは 無効です。」ここで
zipファイルを作成するために、データクラスとメソッドです:
//Archive file containing filename and content as memory stream
public class ArchiveFile {
public string FileName;
public System.IO.MemoryStream FileContent;
}
//Method to retrieve archive files and zip them
public static System.IO.MemoryStream GetFilesAsZip (string[] arrFileNames) {
MemoryStream zipStream = null;
using (zipStream = new MemoryStream()) {
// Retrieve files using above method
ArchiveFile[] retrievedFiles = GetFilesFromArchive(arrFileNames);
// Initialize new ZipArchive on the return object's MemoryStream property
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update, leaveOpen: true)) {
// Write file entries into archive
foreach (ArchiveFile dataFile in retrievedFiles) {
if (dataFile.FileContent != null) {
// Create new ZipArchiveEntry with content
ZipArchiveEntry zipEntry = archive.CreateEntry(dataFile.FileName);
dataFile.FileContent.WriteTo(zipEntry.Open());
}//end if
} // end foreach
} //end using
} //end using
return zipStream;
}//end method
//API to return content to user as an MVC File Content Result
[HttpGet]
public ActionResult DownloadFiles (string [] fileNames) {
FileContentResult data = new FileContentResult(GetFiles(fileNames).GetBuffer(), “application/zip”) { FileDownloadName = “files.zip” };
return data;
} //end method
メモリストリームへの書き込み時に破損がスペースの割り当てとは何かを持っていることがあります。私は "成功"したすべてのジップ(11個以下のファイル)のサイズが259 KBであることに気付きましたが、 "失敗した"ジップ(11個以上のファイル)のサイズは517 KBでした。これは、258.5 KBの倍数であり、特に11ファイルのジップは259 KBジップになりますが、12ファイルのジップは517 KBジップになります。
何ができるかについての洞察はありますか?
どのように共有できますか? – Isma
残念ながら私は個人情報を含んでいません。 – crodriguez
MemoryStreamを正しく使用していません。使用していないMemoryStreamを新規に作成する代わりに、新しいバイト配列を作成して返します。そのzipStream.ToArray()< - 決して適切に処分しないMemoryStreamの代わりに返さなければならないバイト配列に保存します。 –