推薦一個(gè)C#開(kāi)發(fā)的、跨平臺(tái)的解壓縮的開(kāi)源項(xiàng),值得收藏
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
項(xiàng)目簡(jiǎn)介項(xiàng)目結(jié)構(gòu)壓縮核心代碼 protected void Write(CompressionType compressionType, string archive, string archiveToVerifyAgainst, Encoding encoding = null) { using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) { WriterOptions writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true, };
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
using (var writer = WriterFactory.Open(stream, type, writerOptions)) { writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories); } } CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive), Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive))) { ReaderOptions readerOptions = new ReaderOptions();
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
using (var reader = ReaderFactory.Open(NonDisposingStream.create(stream), readerOptions)) { reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true }); } } VerifyFiles(); } 解壓核心代碼 public abstract class ReaderTests : TestBase { protected void Read(string testArchive, CompressionType expectedCompression, ReaderOptions options = null) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
options = options ?? new ReaderOptions();
options.LeaveStreamOpen = true; ReadImpl(testArchive, expectedCompression, options);
options.LeaveStreamOpen = false; ReadImpl(testArchive, expectedCompression, options); VerifyFiles(); }
private void ReadImpl(string testArchive, CompressionType expectedCompression, ReaderOptions options) { using (var file = File.OpenRead(testArchive)) { using (var protectedStream = NonDisposingStream.create(new ForwardOnlyStream(file), throwOnDispose: true)) { using (var testStream = new TestStream(protectedStream)) { using (var reader = ReaderFactory.Open(testStream, options)) { UseReader(reader, expectedCompression); protectedStream.ThrowOnDispose = false; Assert.False(testStream.IsDisposed, "{nameof(testStream)} prematurely closed"); }
// Boolean XOR -- If the stream should be left open (true), then the stream should not be diposed (false) // and if the stream should be closed (false), then the stream should be disposed (true) var message = $"{nameof(options.LeaveStreamOpen)} is set to '{options.LeaveStreamOpen}', so {nameof(testStream.IsDisposed)} should be set to '{!testStream.IsDisposed}', but is set to {testStream.IsDisposed}"; Assert.True(options.LeaveStreamOpen != testStream.IsDisposed, message); } } } }
public void UseReader(IReader reader, CompressionType expectedCompression) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Assert.Equal(expectedCompression, reader.Entry.CompressionType); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } } zip壓縮用法 Write(CompressionType.None, "Zip.none.noEmptyDirs.zip", "Zip.none.noEmptyDirs.zip", Encoding.UTF8); zip解壓用法 Read("Zip.zipx", CompressionType.LZMA);
該文章在 2023/8/16 9:39:40 編輯過(guò) |
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)... |