mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-11-15 17:00:38 +00:00
Locate the PKZIP file data instead of reading it into the model (#102)
* Locate the PKZIP file data instead of reading it into the model Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep only the offset of the PKZIP file data The length is the compressed size from the local file header or the ZIP64 extended field, which the printer already shows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the file data property as a comment for documentation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
@@ -68,5 +69,48 @@ namespace SabreTools.Serialization.Readers.Test
|
||||
var actual = deserializer.Deserialize(data);
|
||||
Assert.Null(actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidStream_FileDataLocated()
|
||||
{
|
||||
Stream? data = CreateArchive();
|
||||
var deserializer = new PKZIP();
|
||||
|
||||
var actual = deserializer.Deserialize(data);
|
||||
Assert.NotNull(actual);
|
||||
|
||||
var localFile = Assert.Single(actual.LocalFiles!);
|
||||
var header = localFile.LocalFileHeader;
|
||||
Assert.Equal(30 + header.FileNameLength + header.ExtraFieldLength, localFile.FileDataOffset);
|
||||
Assert.Single(actual.CentralDirectoryHeaders!);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TruncatedFileData_Null()
|
||||
{
|
||||
byte[] archive = CreateArchive().ToArray();
|
||||
Stream? data = new MemoryStream(archive, 0, 512);
|
||||
var deserializer = new PKZIP();
|
||||
|
||||
var actual = deserializer.Deserialize(data);
|
||||
Assert.Null(actual);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an archive with one stored file of 1000 bytes
|
||||
/// </summary>
|
||||
private static MemoryStream CreateArchive()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
|
||||
{
|
||||
var entry = archive.CreateEntry("file.bin", CompressionLevel.NoCompression);
|
||||
using var entryStream = entry.Open();
|
||||
entryStream.Write([.. Enumerable.Range(0, 1000).Select(i => (byte)i)], 0, 1000);
|
||||
}
|
||||
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user