mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-25 00:15:15 +00:00
Add zstd classes for initial work
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
Zip,
|
||||
Tar,
|
||||
SevenZip,
|
||||
GZip
|
||||
GZip,
|
||||
ZStandard
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
LZip,
|
||||
Xz,
|
||||
Unknown,
|
||||
Deflate64
|
||||
Deflate64,
|
||||
ZStandard,
|
||||
}
|
||||
}
|
||||
50
src/SharpCompress/Common/ZStandard/ZStandardEntry.cs
Normal file
50
src/SharpCompress/Common/ZStandard/ZStandardEntry.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SharpCompress.Common.GZip;
|
||||
|
||||
namespace SharpCompress.Common.ZStandard
|
||||
{
|
||||
public class ZStandardEntry : Entry
|
||||
{
|
||||
private readonly ZStandardFilePart _filePart;
|
||||
|
||||
internal ZStandardEntry(ZStandardFilePart filePart)
|
||||
{
|
||||
_filePart = filePart;
|
||||
}
|
||||
|
||||
public override CompressionType CompressionType => CompressionType.GZip;
|
||||
|
||||
public override long Crc => _filePart.Crc ?? 0;
|
||||
|
||||
public override string Key => _filePart.FilePartName;
|
||||
|
||||
public override string? LinkTarget => null;
|
||||
|
||||
public override long CompressedSize => 0;
|
||||
|
||||
public override long Size => _filePart.UncompressedSize ?? 0;
|
||||
|
||||
public override DateTime? LastModifiedTime => _filePart.DateModified;
|
||||
|
||||
public override DateTime? CreatedTime => null;
|
||||
|
||||
public override DateTime? LastAccessedTime => null;
|
||||
|
||||
public override DateTime? ArchivedTime => null;
|
||||
|
||||
public override bool IsEncrypted => false;
|
||||
|
||||
public override bool IsDirectory => false;
|
||||
|
||||
public override bool IsSplitAfter => false;
|
||||
|
||||
internal override IEnumerable<FilePart> Parts => _filePart.AsEnumerable<FilePart>();
|
||||
|
||||
internal static IEnumerable<GZipEntry> GetEntries(Stream stream, OptionsBase options)
|
||||
{
|
||||
yield return new GZipEntry(new GZipFilePart(stream, options.ArchiveEncoding));
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/SharpCompress/Common/ZStandard/ZStandardFilePart.cs
Normal file
37
src/SharpCompress/Common/ZStandard/ZStandardFilePart.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace SharpCompress.Common.ZStandard
|
||||
{
|
||||
internal sealed class ZStandardFilePart : FilePart
|
||||
{
|
||||
private string _name = "";
|
||||
private readonly Stream _stream;
|
||||
|
||||
internal ZStandardFilePart(Stream stream, ArchiveEncoding archiveEncoding)
|
||||
: base(archiveEncoding)
|
||||
{
|
||||
_stream = stream;
|
||||
EntryStartPosition = stream.Position;
|
||||
}
|
||||
|
||||
internal long EntryStartPosition { get; }
|
||||
|
||||
internal DateTime? DateModified { get; private set; }
|
||||
internal int? Crc { get; private set; }
|
||||
internal int? UncompressedSize { get; private set; }
|
||||
|
||||
internal override string FilePartName => _name!;
|
||||
|
||||
internal override Stream GetCompressedStream()
|
||||
{
|
||||
return new DecompressionStream(_stream);
|
||||
}
|
||||
|
||||
internal override Stream GetRawStream()
|
||||
{
|
||||
return _stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/SharpCompress/Common/ZStandard/ZStandardVolume.cs
Normal file
23
src/SharpCompress/Common/ZStandard/ZStandardVolume.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.IO;
|
||||
using SharpCompress.Readers;
|
||||
|
||||
namespace SharpCompress.Common.ZStandard
|
||||
{
|
||||
public class ZStandardVolume : Volume
|
||||
{
|
||||
public ZStandardVolume(Stream stream, ReaderOptions options)
|
||||
: base(stream, options)
|
||||
{
|
||||
}
|
||||
|
||||
public ZStandardVolume(FileInfo fileInfo, ReaderOptions options)
|
||||
: base(fileInfo.OpenRead(), options)
|
||||
{
|
||||
options.LeaveStreamOpen = false;
|
||||
}
|
||||
|
||||
public override bool IsFirstVolume => true;
|
||||
|
||||
public override bool IsMultiVolume => true;
|
||||
}
|
||||
}
|
||||
32
src/SharpCompress/Readers/ZStandard/ZStandardReader.cs
Normal file
32
src/SharpCompress/Readers/ZStandard/ZStandardReader.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.ZStandard;
|
||||
|
||||
namespace SharpCompress.Readers.Tar
|
||||
{
|
||||
public class ZStandardReader : AbstractReader<ZStandardEntry, ZStandardVolume>
|
||||
{
|
||||
private readonly CompressionType compressionType;
|
||||
|
||||
internal ZStandardReader(Stream stream, ReaderOptions options, CompressionType compressionType)
|
||||
: base(options, ArchiveType.Tar)
|
||||
{
|
||||
this.compressionType = compressionType;
|
||||
Volume = new ZStandardVolume(stream, options);
|
||||
}
|
||||
|
||||
public override ZStandardVolume Volume { get; }
|
||||
|
||||
protected override Stream RequestInitialStream()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IEnumerable<ZStandardEntry> GetEntries(Stream stream)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
61
src/SharpCompress/Writers/ZStandard/ZStandardWriter.cs
Normal file
61
src/SharpCompress/Writers/ZStandard/ZStandardWriter.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.Deflate;
|
||||
using SharpCompress.IO;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace SharpCompress.Writers.ZStandard
|
||||
{
|
||||
public static class ZStandard
|
||||
{
|
||||
public static readonly byte[] Magic = { 0xFD, 0x2F, 0xB5, 0x28};
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FrameHeaderDescriptor
|
||||
{
|
||||
Dictionary_ID_Flag = 0,
|
||||
Dictionary_ID_Flag2 = 1,
|
||||
Content_Checksum_Flag = 2,
|
||||
Single_Segment_Flag = 5,
|
||||
Frame_Content_Size_Flag = 6,
|
||||
Frame_Content_Size_Flag2 = 7
|
||||
|
||||
}
|
||||
|
||||
public class FrameHeader
|
||||
{
|
||||
//private byte Descriptor = 0x0;
|
||||
}
|
||||
public sealed class ZStandardWriter : AbstractWriter
|
||||
{
|
||||
|
||||
public ZStandardWriter(Stream destination, ZStandardWriterOptions? options = null)
|
||||
: base(ArchiveType.ZStandard, options ?? new ZStandardWriterOptions())
|
||||
{
|
||||
if (WriterOptions.LeaveStreamOpen)
|
||||
{
|
||||
destination = new NonDisposingStream(destination);
|
||||
}
|
||||
InitalizeStream(new CompressionStream(destination));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
if (isDisposing)
|
||||
{
|
||||
OutputStream.Dispose();
|
||||
}
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
||||
public override void Write(string filename, Stream source, DateTime? modificationTime)
|
||||
{
|
||||
GZipStream stream = (GZipStream)OutputStream;
|
||||
stream.FileName = filename;
|
||||
stream.LastModified = modificationTime;
|
||||
source.TransferTo(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.Deflate;
|
||||
|
||||
namespace SharpCompress.Writers.ZStandard
|
||||
{
|
||||
public class ZStandardWriterOptions : WriterOptions
|
||||
{
|
||||
public ZStandardWriterOptions()
|
||||
: base(CompressionType.ZStandard)
|
||||
{
|
||||
}
|
||||
|
||||
internal ZStandardWriterOptions(WriterOptions options)
|
||||
: base(options.CompressionType)
|
||||
{
|
||||
LeaveStreamOpen = options.LeaveStreamOpen;
|
||||
ArchiveEncoding = options.ArchiveEncoding;
|
||||
|
||||
var writerOptions = options as ZStandardWriterOptions;
|
||||
if (writerOptions != null)
|
||||
{
|
||||
CompressionLevel = writerOptions.CompressionLevel;
|
||||
}
|
||||
}
|
||||
|
||||
public CompressionLevel CompressionLevel { get; set; } = CompressionLevel.Default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user