Replies: 2 comments
-
@dotnet/area-system-io-compression for better insights. |
Beta Was this translation helpful? Give feedback.
-
There's something wrong in your code to extract data out of the MemoryStream. Running your code as-is reproduces the private static void Main()
{
List<byte> bytes = new();
using (Stream stream = new ListWritingStream(bytes))
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
ZipArchiveEntry entry = archive.CreateEntry("entry.csv");
using (StreamWriter writer = new StreamWriter(entry.Open(), Encoding.UTF8, leaveOpen: true))
{
writer.WriteLine("Val1;Val2;Val3");
writer.WriteLine("Val4;Val5;Val6");
}
}
using (Stream stream = new MemoryStream(bytes.ToArray()))
using (ZipArchive archive = new ZipArchive(stream))
{
Span<byte> buf = stackalloc byte[1024];
foreach (ZipArchiveEntry entry in archive.Entries)
{
Console.WriteLine($"{entry.FullName} -- {entry.Length} byte(s)");
if (entry.Length <= buf.Length)
{
int read;
using (Stream entryStream = entry.Open())
{
read = entryStream.ReadAtLeast(buf, (int)entry.Length);
}
Console.WriteLine("---");
Console.WriteLine(Encoding.UTF8.GetString(buf.Slice(0, read)));
Console.WriteLine("---");
}
}
}
}
private class ListWritingStream : Stream
{
private readonly List<byte> _list;
public ListWritingStream(List<byte> list)
{
_list = list;
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new InvalidOperationException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new InvalidOperationException();
}
public override void SetLength(long value)
{
throw new InvalidOperationException();
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = 0; i < count; i++)
{
_list.Add(buffer[checked(offset + i)]);
}
}
public override void Write(ReadOnlySpan<byte> buffer)
{
foreach (byte b in buffer)
{
_list.Add(b);
}
}
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => throw new InvalidOperationException();
public override long Position
{
get => throw new InvalidOperationException();
set => throw new InvalidOperationException();
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello.
I got problem with splitting content of ZipArchiveEntry while sending it to other service.
Context: i have service with zip creating function. I got some data from third party service, generated csv file based on this data and put it into zip archive. After that, i got memorystream (which is parent for zip), read it till the end, got all contents from there, send it to another service and clean mother-stream. In my case this is necessary because there is huge possibility that data from third party services will be huge, so i need to save opportunity for chunk sending. After closing zip with using, i send eocd bytes. But when i'm trying to recreate zip archive from those bytes, i got an exception that number of entries in CD doesn't corresponding with the actual number.
And here is my question. Is it possible to split ZipArchiveEntry content while sending it or processing it? Sole purpose of this action is reduce memory consuming.
I wrote sample. There is no sending or other stuff. It is just converting those bytes to Base64 there and back.
Beta Was this translation helpful? Give feedback.
All reactions