diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index c7e861d6..64391eca 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -84,16 +84,7 @@ namespace SharpCompress.Archives.Zip StreamingZipHeaderFactory headerFactory = new(password, new ArchiveEncoding()); try { - var headers = headerFactory.ReadStreamHeader(stream); - ZipHeader? header = null; - await foreach (ZipHeader zipHeader in headers) - { - if (zipHeader.ZipHeaderType == ZipHeaderType.Split) - { - header = zipHeader; - break; - } - } + ZipHeader? header = await headerFactory.ReadStreamHeader(stream).FirstOrDefaultAsync(x => x.ZipHeaderType != ZipHeaderType.Split); if (header is null) { return false; diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs index eb554b44..31b3f61c 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.Buffers.Binary; using System.IO; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SharpCompress.Common.Tar.Headers @@ -53,7 +54,7 @@ namespace SharpCompress.Common.Tar.Headers } else { - WriteStringBytes(ArchiveEncoding.Encode(Name), buffer.Memory.Span, 100); + WriteStringBytes(ArchiveEncoding.Encode(Name), buffer.Memory, 100); WriteOctalBytes(Size, buffer.Memory.Span, 124, 12); var time = (long)(LastModifiedTime.ToUniversalTime() - EPOCH).TotalSeconds; WriteOctalBytes(time, buffer.Memory.Span, 136, 12); @@ -68,10 +69,10 @@ namespace SharpCompress.Common.Tar.Headers } } - int crc = RecalculateChecksum(buffer.Memory.Span); + int crc = RecalculateChecksum(buffer.Memory); WriteOctalBytes(crc, buffer.Memory.Span, 148, 8); - await output.WriteAsync(buffer.Memory); + await output.WriteAsync(buffer.Memory.Slice(0, BLOCK_SIZE)); if (nameByteCount > 100) { @@ -91,7 +92,7 @@ namespace SharpCompress.Common.Tar.Headers private async Task WriteLongFilenameHeaderAsync(Stream output) { byte[] nameBytes = ArchiveEncoding.Encode(Name); - await output.WriteAsync(nameBytes, 0, nameBytes.Length); + await output.WriteAsync(nameBytes.AsMemory()); // pad to multiple of BlockSize bytes, and make sure a terminating null is added int numPaddingBytes = BLOCK_SIZE - (nameBytes.Length % BLOCK_SIZE); @@ -102,48 +103,53 @@ namespace SharpCompress.Common.Tar.Headers using var padding = MemoryPool.Shared.Rent(numPaddingBytes); padding.Memory.Span.Clear(); - await output.WriteAsync(padding.Memory); + await output.WriteAsync(padding.Memory.Slice(0, numPaddingBytes)); } - internal bool Read(BinaryReader reader) + internal async ValueTask Read(Stream stream, CancellationToken cancellationToken) { - var buffer = ReadBlock(reader); - if (buffer.Length == 0) + var block = MemoryPool.Shared.Rent(BLOCK_SIZE); + bool readFullyAsync = await stream.ReadFullyAsync(block.Memory.Slice(0, BLOCK_SIZE), cancellationToken); + if (readFullyAsync is false) { return false; } // for symlinks, additionally read the linkname - if (ReadEntryType(buffer) == EntryType.SymLink) + if (ReadEntryType(block.Memory.Span) == EntryType.SymLink) { - LinkName = ArchiveEncoding.Decode(buffer, 157, 100).TrimNulls(); + LinkName = ArchiveEncoding.Decode(block.Memory.Span.Slice(157, 100)).TrimNulls(); } - if (ReadEntryType(buffer) == EntryType.LongName) + if (ReadEntryType(block.Memory.Span) == EntryType.LongName) { - Name = ReadLongName(reader, buffer); - buffer = ReadBlock(reader); + Name = await ReadLongName(stream, block.Memory.Slice(0,BLOCK_SIZE), cancellationToken); + readFullyAsync = await stream.ReadFullyAsync(block.Memory.Slice(0, BLOCK_SIZE), cancellationToken); + if (readFullyAsync is false) + { + return false; + } } else { - Name = ArchiveEncoding.Decode(buffer, 0, 100).TrimNulls(); + Name = ArchiveEncoding.Decode(block.Memory.Span.Slice( 0, 100)).TrimNulls(); } - EntryType = ReadEntryType(buffer); - Size = ReadSize(buffer); + EntryType = ReadEntryType(block.Memory.Span); + Size = ReadSize(block.Memory.Slice(0, BLOCK_SIZE)); //Mode = ReadASCIIInt32Base8(buffer, 100, 7); //UserId = ReadASCIIInt32Base8(buffer, 108, 7); //GroupId = ReadASCIIInt32Base8(buffer, 116, 7); - long unixTimeStamp = ReadAsciiInt64Base8(buffer, 136, 11); + long unixTimeStamp = ReadAsciiInt64Base8(block.Memory.Span.Slice(136, 11)); LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime(); - Magic = ArchiveEncoding.Decode(buffer, 257, 6).TrimNulls(); + Magic = ArchiveEncoding.Decode(block.Memory.Span.Slice( 257, 6)).TrimNulls(); if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic)) { - string namePrefix = ArchiveEncoding.Decode(buffer, 345, 157); + string namePrefix = ArchiveEncoding.Decode(block.Memory.Span.Slice( 345, 157)); namePrefix = namePrefix.TrimNulls(); if (!string.IsNullOrEmpty(namePrefix)) { @@ -158,52 +164,42 @@ namespace SharpCompress.Common.Tar.Headers return true; } - private string ReadLongName(BinaryReader reader, byte[] buffer) + private async ValueTask ReadLongName(Stream reader, ReadOnlyMemory buffer, CancellationToken cancellationToken) { var size = ReadSize(buffer); var nameLength = (int)size; - var nameBytes = reader.ReadBytes(nameLength); + using var nameBytes = MemoryPool.Shared.Rent(nameLength); + await reader.ReadFullyAsync(nameBytes.Memory.Slice(0, nameLength), cancellationToken); var remainingBytesToRead = BLOCK_SIZE - (nameLength % BLOCK_SIZE); // Read the rest of the block and discard the data if (remainingBytesToRead < BLOCK_SIZE) { - reader.ReadBytes(remainingBytesToRead); + using var remaining = MemoryPool.Shared.Rent(remainingBytesToRead); + await reader.ReadFullyAsync(remaining.Memory.Slice(0, remainingBytesToRead), cancellationToken); } - return ArchiveEncoding.Decode(nameBytes, 0, nameBytes.Length).TrimNulls(); + return ArchiveEncoding.Decode(nameBytes.Memory.Span.Slice(0, nameLength)).TrimNulls(); } - private static EntryType ReadEntryType(byte[] buffer) + private static EntryType ReadEntryType(Span buffer) { return (EntryType)buffer[156]; } - private long ReadSize(byte[] buffer) + private long ReadSize(ReadOnlyMemory buffer) { - if ((buffer[124] & 0x80) == 0x80) // if size in binary + if ((buffer.Span[124] & 0x80) == 0x80) // if size in binary { - return BinaryPrimitives.ReadInt64BigEndian(buffer.AsSpan(0x80)); + return BinaryPrimitives.ReadInt64BigEndian(buffer.Span.Slice(0x80)); } - return ReadAsciiInt64Base8(buffer, 124, 11); + return ReadAsciiInt64Base8(buffer.Span.Slice(124, 11)); } - - private static byte[] ReadBlock(BinaryReader reader) + private static void WriteStringBytes(ReadOnlySpan name, Memory buffer, int length) { - byte[] buffer = reader.ReadBytes(BLOCK_SIZE); - - if (buffer.Length != 0 && buffer.Length < BLOCK_SIZE) - { - throw new InvalidOperationException("Buffer is invalid size"); - } - return buffer; - } - - private static void WriteStringBytes(ReadOnlySpan name, Span buffer, int length) - { - name.CopyTo(buffer); + name.CopyTo(buffer.Span.Slice(0)); int i = Math.Min(length, name.Length); - buffer.Slice(i, length - i).Clear(); + buffer.Slice(i, length - i).Span.Clear(); } private static void WriteStringBytes(string name, Span buffer, int offset, int length) @@ -235,19 +231,9 @@ namespace SharpCompress.Common.Tar.Headers } } - private static int ReadAsciiInt32Base8(byte[] buffer, int offset, int count) + private static long ReadAsciiInt64Base8(ReadOnlySpan buffer) { - string s = Encoding.UTF8.GetString(buffer, offset, count).TrimNulls(); - if (string.IsNullOrEmpty(s)) - { - return 0; - } - return Convert.ToInt32(s, 8); - } - - private static long ReadAsciiInt64Base8(byte[] buffer, int offset, int count) - { - string s = Encoding.UTF8.GetString(buffer, offset, count).TrimNulls(); + string s = Encoding.UTF8.GetString(buffer).TrimNulls(); if (string.IsNullOrEmpty(s)) { return 0; @@ -271,38 +257,20 @@ namespace SharpCompress.Common.Tar.Headers (byte)' ', (byte)' ', (byte)' ', (byte)' ' }; - private static int RecalculateChecksum(Span buf) + private static int RecalculateChecksum(Memory buf) { // Set default value for checksum. That is 8 spaces. - eightSpaces.AsSpan(148).CopyTo(buf); + eightSpaces.CopyTo(buf.Slice(148)); // Calculate checksum int headerChecksum = 0; - foreach (byte b in buf) + foreach (byte b in buf.Span) { headerChecksum += b; } return headerChecksum; } - internal static int RecalculateAltChecksum(byte[] buf) - { - eightSpaces.CopyTo(buf, 148); - int headerChecksum = 0; - foreach (byte b in buf) - { - if ((b & 0x80) == 0x80) - { - headerChecksum -= b ^ 0x80; - } - else - { - headerChecksum += b; - } - } - return headerChecksum; - } - public long? DataStartPosition { get; set; } public string Magic { get; set; } diff --git a/src/SharpCompress/Compressors/Deflate/GZipStream.cs b/src/SharpCompress/Compressors/Deflate/GZipStream.cs index f608c8df..ba29c1b3 100644 --- a/src/SharpCompress/Compressors/Deflate/GZipStream.cs +++ b/src/SharpCompress/Compressors/Deflate/GZipStream.cs @@ -27,6 +27,7 @@ // ------------------------------------------------------------------ using System; +using System.Buffers; using System.Buffers.Binary; using System.IO; using System.Text; @@ -418,24 +419,19 @@ namespace SharpCompress.Compressors.Deflate private async Task EmitHeaderAsync() { - byte[]? commentBytes = (Comment is null) ? null - : _encoding.GetBytes(Comment); - byte[]? filenameBytes = (FileName is null) ? null - : _encoding.GetBytes(FileName); - - int cbLength = commentBytes?.Length + 1 ?? 0; - int fnLength = filenameBytes?.Length + 1 ?? 0; + int cbLength = Comment is null ? 0 : _encoding.GetByteCount(Comment); + int fnLength = FileName is null ? 0 : _encoding.GetByteCount(FileName); int bufferLength = 10 + cbLength + fnLength; - var header = new byte[bufferLength]; + using var header = MemoryPool.Shared.Rent(bufferLength); int i = 0; // ID - header[i++] = 0x1F; - header[i++] = 0x8B; + header.Memory.Span[i++] = 0x1F; + header.Memory.Span[i++] = 0x8B; // compression method - header[i++] = 8; + header.Memory.Span[i++] = 8; byte flag = 0; if (Comment != null) { @@ -447,7 +443,7 @@ namespace SharpCompress.Compressors.Deflate } // flag - header[i++] = flag; + header.Memory.Span[i++] = flag; // mtime if (LastModified is null) @@ -456,14 +452,14 @@ namespace SharpCompress.Compressors.Deflate } TimeSpan delta = LastModified.Value - UNIX_EPOCH; var timet = (int)delta.TotalSeconds; - BinaryPrimitives.WriteInt32LittleEndian(header.AsSpan(i), timet); + BinaryPrimitives.WriteInt32LittleEndian(header.Memory.Span.Slice(i), timet); i += 4; // xflg - header[i++] = 0; // this field is totally useless + header.Memory.Span[i++] = 0; // this field is totally useless // OS - header[i++] = 0xFF; // 0xFF == unspecified + header.Memory.Span[i++] = 0xFF; // 0xFF == unspecified // extra field length - only if FEXTRA is set, which it is not. //header[i++]= 0; @@ -472,22 +468,22 @@ namespace SharpCompress.Compressors.Deflate // filename if (fnLength != 0) { - Array.Copy(filenameBytes!, 0, header, i, fnLength - 1); - i += fnLength - 1; - header[i++] = 0; // terminate + _encoding.GetBytes(FileName, header.Memory.Span.Slice(i, fnLength)); + i += fnLength; + header.Memory.Span[i++] = 0; // terminate } // comment if (cbLength != 0) { - Array.Copy(commentBytes!, 0, header, i, cbLength - 1); + _encoding.GetBytes(Comment, header.Memory.Span.Slice(i, cbLength)); i += cbLength - 1; - header[i++] = 0; // terminate + header.Memory.Span[i++] = 0; // terminate } - await _baseStream._stream.WriteAsync(header, 0, header.Length); + await _baseStream._stream.WriteAsync(header.Memory.Slice(0, bufferLength)); - return header.Length; // bytes written + return bufferLength; // bytes written } } } diff --git a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs index a8c59dc3..10c45bea 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs @@ -160,7 +160,7 @@ namespace SharpCompress.Compressors.Deflate } //if (_workingBuffer.Length - _z.AvailableBytesOut > 0) - await _stream.WriteAsync(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut, cancellationToken); + await _stream.WriteAsync(new Memory(_workingBuffer).Slice(0, _workingBuffer.Length - _z.AvailableBytesOut), cancellationToken); done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0; @@ -232,10 +232,10 @@ namespace SharpCompress.Compressors.Deflate // Emit the GZIP trailer: CRC32 and size mod 2^32 using var intBuf = MemoryPool.Shared.Rent(4); BinaryPrimitives.WriteInt32LittleEndian(intBuf.Memory.Span, crc.Crc32Result); - await _stream.WriteAsync(intBuf.Memory); + await _stream.WriteAsync(intBuf.Memory.Slice(0,4)); var c2 = (int)(crc.TotalBytesRead & 0x00000000FFFFFFFF); BinaryPrimitives.WriteInt32LittleEndian(intBuf.Memory.Span, c2); - await _stream.WriteAsync(intBuf.Memory); + await _stream.WriteAsync(intBuf.Memory.Slice(0,4)); } else { @@ -414,7 +414,7 @@ namespace SharpCompress.Compressors.Deflate // read the header on the first read using var rented = MemoryPool.Shared.Rent(10); - int n = await _stream.ReadAsync(rented.Memory); + int n = await _stream.ReadAsync(rented.Memory.Slice(0,10)); var header = rented.Memory; // workitem 8501: handle edge case (decompress empty stream) @@ -444,7 +444,7 @@ namespace SharpCompress.Compressors.Deflate var extraLength = (short)(header.Span[0] + header.Span[1] * 256); using var extra = MemoryPool.Shared.Rent(extraLength); - n = await _stream.ReadAsync(extra.Memory); + n = await _stream.ReadAsync(extra.Memory.Slice(0, extraLength)); if (n != extraLength) { throw new ZlibException("Unexpected end-of-file reading GZIP header."); diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index 09538ad7..cb52bbae 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -81,15 +81,21 @@ namespace SharpCompress.Readers } if (entriesForCurrentReadStream is null) { - //TODO: real token? - return await LoadStreamForReading(RequestInitialStream(), cancellationToken); - } - if (!wroteCurrentEntry) + var stream = RequestInitialStream(); + if (stream is null || !stream.CanRead) + { + throw new MultipartStreamRequiredException("File is split into multiple archives: '" + + (Entry?.Key ?? "unknown") + + "'. A new readable stream is required. Use Cancel if it was intended."); + } + entriesForCurrentReadStream = GetEntries(stream, cancellationToken).GetAsyncEnumerator(cancellationToken); + } + else if (!wroteCurrentEntry) { await SkipEntry(cancellationToken); } wroteCurrentEntry = false; - if (await NextEntryForCurrentStream()) + if (await entriesForCurrentReadStream.MoveNextAsync()) { return true; } @@ -97,36 +103,18 @@ namespace SharpCompress.Readers return false; } - protected async Task LoadStreamForReading(Stream stream, CancellationToken cancellationToken) - { - await (entriesForCurrentReadStream?.DisposeAsync() ?? new ValueTask(Task.CompletedTask)); - if (stream is null || !stream.CanRead) - { - throw new MultipartStreamRequiredException("File is split into multiple archives: '" - + (Entry?.Key ?? "unknown") + - "'. A new readable stream is required. Use Cancel if it was intended."); - } - entriesForCurrentReadStream = GetEntries(stream, cancellationToken).GetAsyncEnumerator(cancellationToken); - return await (entriesForCurrentReadStream?.MoveNextAsync() ?? new ValueTask(Task.FromResult(false))); - } - protected virtual Stream RequestInitialStream() { return Volume.Stream; } - internal virtual async ValueTask NextEntryForCurrentStream() - { - return await (entriesForCurrentReadStream?.MoveNextAsync() ?? new ValueTask(Task.FromResult(false))); - } - protected abstract IAsyncEnumerable GetEntries(Stream stream, CancellationToken cancellationToken); #region Entry Skip/Write private async ValueTask SkipEntry(CancellationToken cancellationToken) { - if (Entry?.IsDirectory == true) + if (Entry?.IsDirectory != true) { await SkipAsync(cancellationToken); } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 3c3c0319..8396a5d4 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -78,12 +78,12 @@ namespace SharpCompress.Readers return new TarReader(rewindableStream, options, CompressionType.LZip); } } - rewindableStream.Rewind(false); - /* if (RarArchive.IsRarFile(rewindableStream, options)) - { - rewindableStream.Rewind(true); - return RarReader.Open(rewindableStream, options); - } */ + /* rewindableStream.Rewind(false); + if (RarArchive.IsRarFile(rewindableStream, options)) + { + rewindableStream.Rewind(true); + return RarReader.Open(rewindableStream, options); + } */ rewindableStream.Rewind(false); if (await TarArchive.IsTarFileAsync(rewindableStream)) @@ -95,12 +95,12 @@ namespace SharpCompress.Readers if (XZStream.IsXZStream(rewindableStream)) { rewindableStream.Rewind(true); - /* XZStream testStream = new XZStream(rewindableStream); - if (TarArchive.IsTarFile(testStream)) - { - rewindableStream.Rewind(true); - return new TarReader(rewindableStream, options, CompressionType.Xz); - } */ + XZStream testStream = new XZStream(rewindableStream); + if (await TarArchive.IsTarFileAsync(testStream)) + { + rewindableStream.Rewind(true); + return new TarReader(rewindableStream, options, CompressionType.Xz); + } } throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ"); } diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index 27845d16..9abd1e23 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -57,14 +57,19 @@ namespace SharpCompress.Writers.Tar InitializeStream(destination); } - public override async Task WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default) + public override ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default) { - if (!source.CanSeek) + return WriteAsync(filename, source, modificationTime, null, cancellationToken); + } + + public async ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, long? size, CancellationToken cancellationToken = default) + { + if (!source.CanSeek && size == null) { throw new ArgumentException("Seekable stream is required if no size is given."); } - long realSize = source.Length; + long realSize = size ?? source.Length; TarHeader header = new(WriterOptions.ArchiveEncoding); @@ -72,8 +77,8 @@ namespace SharpCompress.Writers.Tar header.Name = NormalizeFilename(filename); header.Size = realSize; await header.WriteAsync(OutputStream); - var size = await source.TransferToAsync(OutputStream); - await PadTo512Async(size, false); + size = await source.TransferToAsync(OutputStream, cancellationToken); + await PadTo512Async(size.Value, false); } private string NormalizeFilename(string filename) @@ -99,7 +104,7 @@ namespace SharpCompress.Writers.Tar zeros = 512 - zeros; using var zeroBuffer = MemoryPool.Shared.Rent(zeros); zeroBuffer.Memory.Span.Clear(); - await OutputStream.WriteAsync(zeroBuffer.Memory); + await OutputStream.WriteAsync(zeroBuffer.Memory.Slice(0, zeros)); } protected override async ValueTask DisposeAsyncCore() diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 2712c260..e865901e 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -164,7 +164,7 @@ namespace SharpCompress.Writers.Zip // TODO: Use stackalloc when we exclusively support netstandard2.1 or higher using var intBuf = MemoryPool.Shared.Rent(4); BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, ZipHeaderFactory.ENTRY_HEADER_BYTES); - await OutputStream.WriteAsync(intBuf.Memory, cancellationToken); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,4), cancellationToken); if (explicitZipCompressionInfo == ZipCompressionMethod.Deflate) { if (OutputStream.CanSeek && useZip64) @@ -172,14 +172,14 @@ namespace SharpCompress.Writers.Zip using var buf1 = MemoryPool.Shared.Rent(2); buf1.Memory.Span[0] = 45; buf1.Memory.Span[1] = 0; - await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //smallest allowed version for zip64 + await OutputStream.WriteAsync(buf1.Memory.Slice(0, 2), cancellationToken); //smallest allowed version for zip64 } else { using var buf1 = MemoryPool.Shared.Rent(2); buf1.Memory.Span[0] = 20; buf1.Memory.Span[1] = 0; - await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //older version which is more compatible + await OutputStream.WriteAsync(buf1.Memory.Slice(0, 2), cancellationToken); //older version which is more compatible } } else @@ -187,7 +187,7 @@ namespace SharpCompress.Writers.Zip using var buf1 = MemoryPool.Shared.Rent(2); buf1.Memory.Span[0] = 63; buf1.Memory.Span[1] = 0; - await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //version says we used PPMd or LZMA + await OutputStream.WriteAsync(buf1.Memory.Slice(0, 2), cancellationToken); //version says we used PPMd or LZMA } HeaderFlags flags = Equals(WriterOptions.ArchiveEncoding.GetEncoding(), Encoding.UTF8) ? HeaderFlags.Efs : 0; if (!OutputStream.CanSeek) @@ -205,12 +205,12 @@ namespace SharpCompress.Writers.Zip BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)explicitZipCompressionInfo); await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken); // zipping method BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, zipWriterEntryOptions.ModificationDateTime.DateTimeToDosTime()); - await OutputStream.WriteAsync(intBuf.Memory, cancellationToken); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,4), cancellationToken); // zipping date and time using var buf2 = MemoryPool.Shared.Rent(12); buf2.Memory.Span.Clear(); - await OutputStream.WriteAsync(buf2.Memory, cancellationToken); + await OutputStream.WriteAsync(buf2.Memory.Slice(0, 12), cancellationToken); // unused CRC, un/compressed size, updated later BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)encodedFilename.Length); @@ -224,13 +224,13 @@ namespace SharpCompress.Writers.Zip BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)extralength); await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken);// extra length - await OutputStream.WriteAsync(encodedFilename, 0, encodedFilename.Length, cancellationToken); + await OutputStream.WriteAsync(encodedFilename.AsMemory(), cancellationToken); if (extralength != 0) { using var buf3 = MemoryPool.Shared.Rent(extralength); buf2.Memory.Span.Clear(); - await OutputStream.WriteAsync(buf3.Memory, cancellationToken); // reserve space for zip64 data + await OutputStream.WriteAsync(buf3.Memory.Slice(0, extralength), cancellationToken); // reserve space for zip64 data entry.Zip64HeaderOffset = (ushort)(6 + 2 + 2 + 4 + 12 + 2 + 2 + encodedFilename.Length); } @@ -241,11 +241,11 @@ namespace SharpCompress.Writers.Zip { using var intBuf = MemoryPool.Shared.Rent(4); BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, crc); - await OutputStream.WriteAsync(intBuf.Memory); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 4)); BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, compressed); - await OutputStream.WriteAsync(intBuf.Memory); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 4)); BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, uncompressed); - await OutputStream.WriteAsync(intBuf.Memory); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 4)); } private async Task WriteEndRecordAsync(ulong size) @@ -270,7 +270,7 @@ namespace SharpCompress.Writers.Zip await OutputStream.WriteAsync(s); BinaryPrimitives.WriteUInt64LittleEndian(intBuf.Memory.Span, (ulong)recordlen); - await OutputStream.WriteAsync(intBuf.Memory);// Size of zip64 end of central directory record + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8));// Size of zip64 end of central directory record BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, 0); await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 2)); // Made by BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, 45); @@ -282,12 +282,12 @@ namespace SharpCompress.Writers.Zip // TODO: entries.Count is int, so max 2^31 files BinaryPrimitives.WriteUInt64LittleEndian(intBuf.Memory.Span, (ulong)entries.Count); - await OutputStream.WriteAsync(intBuf.Memory); // Entries in this disk - await OutputStream.WriteAsync(intBuf.Memory); // Total entries + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); // Entries in this disk + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); // Total entries BinaryPrimitives.WriteUInt64LittleEndian(intBuf.Memory.Span, size); - await OutputStream.WriteAsync(intBuf.Memory); // Central Directory size + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); // Central Directory size BinaryPrimitives.WriteUInt64LittleEndian(intBuf.Memory.Span, (ulong)streamPosition); - await OutputStream.WriteAsync(intBuf.Memory); // Disk offset + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); // Disk offset // Write zip64 end of central directory locator s = intBuf.Memory.Slice(0, 4); @@ -300,7 +300,7 @@ namespace SharpCompress.Writers.Zip BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, 0); await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 4)); // Entry disk BinaryPrimitives.WriteUInt64LittleEndian(intBuf.Memory.Span, (ulong)streamPosition + size); - await OutputStream.WriteAsync(intBuf.Memory); // Offset to the zip64 central directory + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); // Offset to the zip64 central directory BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, 0); await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 4)); // Number of disks @@ -315,7 +315,7 @@ namespace SharpCompress.Writers.Zip x.Span[1] = 75; x.Span[2] = 5; x.Span[3] = 6; - await OutputStream.WriteAsync(intBuf.Memory); + await OutputStream.WriteAsync(intBuf.Memory.Slice(0,8)); BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)entries.Count); await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 2)); await OutputStream.WriteAsync(intBuf.Memory.Slice(0, 2));//TODO: this is twice? @@ -499,7 +499,7 @@ namespace SharpCompress.Writers.Zip using var intBuf = MemoryPool.Shared.Rent(4); BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, ZipHeaderFactory.POST_DATA_DESCRIPTOR); - await originalStream.WriteAsync(intBuf.Memory); + await originalStream.WriteAsync(intBuf.Memory.Slice(0,4)); await writer.WriteFooterAsync(entry.Crc, compressedvalue, decompressedvalue);