解壓縮對于我們?nèi)粘9ぷ魈?jīng)常用到了,畢竟它有諸多好處,比如節(jié)省空間、方便管理傳輸、保密作用等等。
而對于我們開發(fā)人員來說,項(xiàng)目開發(fā)也是經(jīng)常需要用到,今天就給大家推薦用一個可以用于壓縮、解壓、解密,并支持多種壓縮類型和格式的開源項(xiàng)目。
項(xiàng)目簡介
這是一個純C#壓縮庫,用于.NET Standard 2.0、2.1、.NET Core 3.1和.NET 5.0,支持格式有zip/tar/bzip2/gzip/lzip,功能實(shí)現(xiàn)有解壓縮rar, 解壓縮7zip, 解壓縮zip, 解壓縮tar解壓縮bzip2, 解壓縮gzip, 解壓縮lzip。
項(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
});
}
}
}
}
Write(CompressionType.None, "Zip.none.noEmptyDirs.zip", "Zip.none.noEmptyDirs.zip", Encoding.UTF8);
Read("Zip.zipx", CompressionType.LZMA);
RAR 5 解密
7Zip 寫入
Zip64 解壓縮
多卷Zip支持
項(xiàng)目地址:https://github.com/adamhathcock/sharpcompress
該文章在 2024/8/22 9:14:53 編輯過