public static CompressedPortion Compress(StreamPortion portion) { using (var uncompressedStream = new MemoryStream(portion.Data)) { using (var compressedStream = new MemoryStream()) { using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) { uncompressedStream.CopyTo(zipStream); } return new CompressedPortion(portion.StartPostion, compressedStream.ToArray()); } } }
private static void PopulateFolderFileHierarchy(BinaryReaderEx br, FolderEntry parentFolder, BlockHierarchy hierarchy, LIFFFolderEntry folderEntry) { if (hierarchy.ChildCount != folderEntry.EntryCount) { throw new IOException("The file is not a valid LIF. Entry count mismatch."); } for (int i = 0; i < folderEntry.EntryCount; i++) { var expectedBlock = hierarchy.Childs[i]; short entryType = br.ReadInt16(); br.BaseStream.Skip(-2); LifEntry entry; if (entryType == 1) { var folderInfo = br.ReadStruct <LIFFFolderEntry>(); entry = new FolderEntry(folderInfo.Filename); PopulateFolderFileHierarchy(br, (FolderEntry)entry, expectedBlock, folderInfo); } else if (entryType == 2) { var fileInfo = br.ReadStruct <LIFFFileEntry>(); var dataStream = new StreamPortion(br.BaseStream, expectedBlock.PositionInStream + LIFFBLOCK_SIZE, fileInfo.FileSize - LIFFBLOCK_SIZE); entry = new FileEntry(dataStream, fileInfo.Filename); ((FileEntry)entry).SetFileAttributes( DateTime.FromFileTime(fileInfo.Created), DateTime.FromFileTime(fileInfo.Modified), DateTime.FromFileTime(fileInfo.Accessed)); } else { throw new IOException("The file is not a valid LIF."); } parentFolder.Entries.Add(entry); } }
private static bool TryRead(Stream stream, int length, out StreamPortion chunk) { chunk = null; var streamPosition = stream.Position; using (var memStream = new MemoryStream()) { if (stream.CopyAmountTo(memStream, length) == 0) return false; chunk = new StreamPortion(streamPosition, memStream.ToArray()); return true; } }