Skip to content

Instantly share code, notes, and snippets.

@lynzrand
Created August 26, 2020 02:28
Show Gist options
  • Save lynzrand/aeba1835762c9ab7a4e6eea1466c74af to your computer and use it in GitHub Desktop.
Save lynzrand/aeba1835762c9ab7a4e6eea1466c74af to your computer and use it in GitHub Desktop.
Splitting stream & reading stuff inside
using System;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
using System.Text.Unicode;
using System.Text;
using SharpCompress.Readers;
using MicroKnights.IO.Streams;
class Program
{
static void Main(string[] args)
{
AsyncMain(args[0], args[1]).Wait();
}
static async Task AsyncMain(string i, string o)
{
using var f = new FileStream(i, FileMode.Open);
var buf = new byte[131072];
using var fdest = new FileStream(o, FileMode.OpenOrCreate);
Console.WriteLine("Opened");
using var input = new ReadableSplitStream(f);
using var input1 = input.GetForwardReadOnlyStream();
using (var input2 = input.GetForwardReadOnlyStream())
{
await input.StartReadAHead();
var task1 = Task.Run(async () =>
{
await input1.CopyToAsync(fdest);
});
var task2 = Task.Run(async () =>
{
await ReadArchive(input2);
});
await Task.WhenAll(task1, task2);
}
}
static async Task ReadArchive(Stream underlying)
{
var opt = new ReaderOptions();
opt.LeaveStreamOpen = true;
var r = ReaderFactory.Open(underlying, opt);
Console.WriteLine($"kind: {r.ArchiveType}");
Console.WriteLine(underlying.Length);
while (r.MoveToNextEntry())
{
var entry = r.Entry;
Console.WriteLine($"Found entry: {entry.Key} with size {entry.Size}");
if (entry.Key == "test.txt")
{
var len = entry.Size;
var buf = new byte[4096];
var s = r.OpenEntryStream();
var n = await s.ReadAsync(buf);
while (n > 0)
{
Console.Write(UTF8Encoding.UTF8.GetString(buf));
n = await s.ReadAsync(buf);
}
Console.WriteLine();
}
}
var end_buf = new byte[4096];
int nx;
do
nx = await underlying.ReadAsync(end_buf);
while (nx > 0);
Console.WriteLine($"Read end");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MicroKnights.IO.SplitStream" Version="1.0.0" />
<PackageReference Include="sharpcompress" Version="0.26.0" />
<PackageReference Include="SharpZipLib" Version="1.2.0" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment