public async Task <IActionResult> SiaZip(string hash, [FromQuery] string path) { var zipStream = new System.IO.Compression.HttpZipStream("https://siasky.net/" + hash); if (await zipStream.GetContentLengthAsync() == -1) { return(NotFound("No such Sia hash.")); } using (zipStream) { await zipStream.GetContentLengthAsync(); string content = ""; var entryList = await zipStream.GetEntriesAsync(); bool found = false; foreach (var e in entryList) { if (e.FileName != path) { continue; } found = true; await zipStream.ExtractAsync(e, async (entryStream) => { byte[] buffer = new byte[4096]; while (true) { int result = await entryStream.ReadAsync(buffer, content.Length, 4096); if (result == 0) { break; } content += buffer.ToString(); } }); } if (!found) { return(NotFound("No such file in archive.")); } System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition { FileName = path, Inline = false, }; Response.Headers.Add("Content-Disposition", cd.ToString()); return(View(content)); } }
public async void ExampleStream_ContentLength_MustBe_9702kbytes() { using (var streamZip = new HttpZipStream(httpUrl)) { var contentLength = await streamZip.GetContentLengthAsync(); Assert.Equal(9935427, contentLength); } }
public async void ExampleStream_Entries_MustHave_36items() { using (var streamZip = new HttpZipStream(httpUrl)) { var contentLength = await streamZip.GetContentLengthAsync(); var entryList = await streamZip.GetEntriesAsync(); Assert.Equal(36, entryList.Count); } }
public async void ExampleStream_LargerEntry_MustBe_0001_With_347kbytes() { using (var streamZip = new HttpZipStream(httpUrl)) { var contentLength = await streamZip.GetContentLengthAsync(); var entryList = await streamZip.GetEntriesAsync(); var largerEntry = entryList .OrderByDescending(x => x.CompressedSize) .Take(1) .FirstOrDefault(); Assert.Equal("Blue Beetle [1967] #01 - 0001.jpg", largerEntry.FileName); Assert.Equal(355736, largerEntry.CompressedSize); } }
public async void ExampleStream_SmallerEntryExtraction_MustResult_MemoryStream_With_227kbytes() { using (var streamZip = new HttpZipStream(httpUrl)) { var contentLength = await streamZip.GetContentLengthAsync(); var entryList = await streamZip.GetEntriesAsync(); var smallerEntry = entryList .OrderBy(x => x.CompressedSize) .Take(1) .FirstOrDefault(); long memoryStreamLength = 0; await streamZip.ExtractAsync(smallerEntry, (MemoryStream memoryStream) => { memoryStreamLength = memoryStream.Length; }); Assert.Equal(232660, memoryStreamLength); } }