Support ancient .NET in FileTypes

This commit is contained in:
Matt Nadareski
2024-02-28 21:59:13 -05:00
parent 080c8a749b
commit de59d0252c
49 changed files with 597 additions and 59 deletions

View File

@@ -6,7 +6,7 @@ namespace Compress
{
public static class CompressUtils
{
public static void CreateDirForFile(string sFilename)
{
string strTemp = Path.GetDirectoryName(sFilename);
@@ -32,7 +32,10 @@ namespace Compress
{
if (enc != null)
return;
#if NET462_OR_GREATER || NETCOREAPP
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
enc = Encoding.GetEncoding(437);
}

View File

@@ -7,7 +7,9 @@ using Compress.SevenZip.Structure;
using Compress.Support.Compression.BZip2;
using Compress.Support.Compression.LZMA;
using Compress.Support.Compression.PPmd;
#if NET462_OR_GREATER || NETCOREAPP
using Compress.Support.Compression.zStd;
#endif
using Compress.Support.Filters;
using FileStream = RVIO.FileStream;
@@ -163,9 +165,11 @@ namespace Compress.SevenZip
case DecompressType.BCJ2:
coder.DecoderStream = new BCJ2Filter(inputCoders[0], inputCoders[1], inputCoders[2], inputCoders[3]);
break;
#if NET462_OR_GREATER || NETCOREAPP
case DecompressType.ZSTD:
coder.DecoderStream =new zStdSharp(inputCoders[0]);
coder.DecoderStream = new zStdSharp(inputCoders[0]);
break;
#endif
default:
return ZipReturn.ZipDecodeError;
}

View File

@@ -26,7 +26,7 @@ namespace Compress.SevenZip
ZipStatus = ZipStatus.TrrntZip;
}
private bool IsRomVault7Z(long testBaseOffset,ulong testHeaderPos,ulong testHeaderLength,uint testHeaderCRC)
private bool IsRomVault7Z(long testBaseOffset, ulong testHeaderPos, ulong testHeaderLength, uint testHeaderCRC)
{
long length = _zipFs.Length;
if (length < 32)
@@ -39,8 +39,8 @@ namespace Compress.SevenZip
byte[] rv7Zid = Util.Enc.GetBytes(sig);
byte[] header = new byte[12];
_zipFs.Read(header, 0, 12);
for (int i = 0; i < 12; i++)
{
if (header[i] != rv7Zid[i])
@@ -52,7 +52,11 @@ namespace Compress.SevenZip
uint headerCRC;
ulong headerOffset; // is location of header in file
ulong headerSize;
#if NET20 || NET35 || NET40
using (BinaryReader br = new(_zipFs, Encoding.UTF8))
#else
using (BinaryReader br = new(_zipFs, Encoding.UTF8, true))
#endif
{
headerCRC = br.ReadUInt32();
headerOffset = br.ReadUInt64();
@@ -62,7 +66,7 @@ namespace Compress.SevenZip
if (headerCRC != testHeaderCRC)
return false;
if (headerOffset != testHeaderPos+(ulong)testBaseOffset)
if (headerOffset != testHeaderPos + (ulong)testBaseOffset)
return false;
return headerSize == testHeaderLength;

View File

@@ -57,7 +57,11 @@ namespace Compress.SevenZip
_signatureHeader = new SignatureHeader();
_header = new Header();
#if NET20 || NET35 || NET40
using (BinaryWriter bw = new(_zipFs, Encoding.UTF8))
#else
using (BinaryWriter bw = new(_zipFs, Encoding.UTF8, true))
#endif
{
_signatureHeader.Write(bw);
}
@@ -179,6 +183,7 @@ namespace Compress.SevenZip
_compressStream = lzs;
break;
#if NET462_OR_GREATER || NETCOREAPP
case SevenZipCompressType.zstd:
ZstdSharp.CompressionStream zss = new(_zipFs, 19);
@@ -186,6 +191,7 @@ namespace Compress.SevenZip
newStream.Properties = new byte[] { 1, 5, 19, 0, 0 };
_compressStream = zss;
break;
#endif
case SevenZipCompressType.uncompressed:
newStream.Method = new byte[] { 0 };

View File

@@ -130,7 +130,11 @@ namespace Compress.SevenZip
byte[] newHeaderByte;
using (Stream headerMem = new MemoryStream())
{
#if NET20 || NET35 || NET40
using BinaryWriter headerBw = new(headerMem, Encoding.UTF8);
#else
using BinaryWriter headerBw = new(headerMem, Encoding.UTF8, true);
#endif
_header.WriteHeader(headerBw);
newHeaderByte = new byte[headerMem.Length];
@@ -140,7 +144,7 @@ namespace Compress.SevenZip
uint mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length);
#region Header Compression
#region Header Compression
long packedHeaderPos = _zipFs.Position;
LzmaEncoderProperties ep = new(true, GetDictionarySizeFromUncompressedSize((ulong)newHeaderByte.Length), 64);
LzmaStream lzs = new(ep, false, _zipFs);
@@ -177,7 +181,11 @@ namespace Compress.SevenZip
using (Stream headerMem = new MemoryStream())
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(headerMem, Encoding.UTF8);
#else
using BinaryWriter bw = new(headerMem, Encoding.UTF8, true);
#endif
bw.Write((byte)HeaderProperty.kEncodedHeader);
streamsInfo.WriteHeader(bw);
@@ -186,10 +194,13 @@ namespace Compress.SevenZip
headerMem.Read(newHeaderByte, 0, newHeaderByte.Length);
}
mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length);
#endregion
#endregion
#if NET20 || NET35 || NET40
using (BinaryWriter bw = new(_zipFs, Encoding.UTF8))
#else
using (BinaryWriter bw = new(_zipFs, Encoding.UTF8, true))
#endif
{
ulong headerPosition = (ulong)_zipFs.Position + 32; //tzip header is 32 bytes
WriteRomVault7Zip(bw, headerPosition, (ulong)newHeaderByte.Length, mainHeaderCRC);

View File

@@ -98,7 +98,11 @@ namespace Compress.SevenZip.Structure
byte[] namebyte;
using (MemoryStream nameMem = new())
{
#if NET20 || NET35 || NET40
using BinaryWriter nameBw = new(nameMem, Encoding.UTF8);
#else
using BinaryWriter nameBw = new(nameMem, Encoding.UTF8, true);
#endif
nameBw.Write((byte)0); //not external
foreach (string name in Names)
{

View File

@@ -49,7 +49,11 @@ namespace Compress.SevenZip.Structure
{
header = null;
#if NET20 || NET35 || NET40
using BinaryReader br = new(stream, Encoding.UTF8);
#else
using BinaryReader br = new(stream, Encoding.UTF8, true);
#endif
HeaderProperty hp = (HeaderProperty)br.ReadByte();
switch (hp)
{

View File

@@ -20,7 +20,11 @@ namespace Compress.SevenZip.Structure
public bool Read(Stream stream)
{
#if NET20 || NET35 || NET40
using BinaryReader br = new(stream, Encoding.UTF8);
#else
using BinaryReader br = new(stream, Encoding.UTF8, true);
#endif
byte[] signatureBytes = br.ReadBytes(6);
if (!signatureBytes.Compare(Signature))
{
@@ -82,7 +86,11 @@ namespace Compress.SevenZip.Structure
byte[] sigHeaderBytes;
using (MemoryStream sigHeaderMem = new())
{
#if NET20 || NET35 || NET40
using BinaryWriter sigHeaderBw = new(sigHeaderMem, Encoding.UTF8);
#else
using BinaryWriter sigHeaderBw = new(sigHeaderMem, Encoding.UTF8, true);
#endif
sigHeaderBw.Write((ulong)((long)headerpos - BaseOffset)); //NextHeaderOffset
sigHeaderBw.Write(headerLength); //NextHeaderSize
sigHeaderBw.Write(headerCRC); //NextHeaderCRC

View File

@@ -1,4 +1,5 @@
using System.IO;
#if NET462_OR_GREATER || NETCOREAPP
using System.IO;
namespace Compress.Support.Compression.zStd
{
@@ -62,3 +63,4 @@ namespace Compress.Support.Compression.zStd
}
}
}
#endif

View File

@@ -72,7 +72,7 @@ namespace Compress.Support.Utils
MessageCallBack?.Invoke($"Extracting {filenameOut}");
string fOut = Path.Combine(outDir, filenameOut.Replace('/', '\\'));
string dOut = Path.GetDirectoryName(fOut);
if (!string.IsNullOrWhiteSpace(dOut) && !Directory.Exists(dOut))
if (!string.IsNullOrEmpty(dOut) && !Directory.Exists(dOut))
Directory.CreateDirectory(dOut);
int errorCode = FileStream.OpenFileWrite(fOut, out Stream sWrite);

View File

@@ -6,7 +6,7 @@ namespace Compress.ThreadReaders
{
public class ThreadCRC : IDisposable
{
private CRC crc;
private CRC crc;
private readonly AutoResetEvent _waitEvent;
private readonly AutoResetEvent _outEvent;
private readonly Thread _tWorker;
@@ -18,7 +18,7 @@ namespace Compress.ThreadReaders
public ThreadCRC()
{
crc=new CRC();
crc = new CRC();
_waitEvent = new AutoResetEvent(false);
_outEvent = new AutoResetEvent(false);
_finished = false;
@@ -31,8 +31,10 @@ namespace Compress.ThreadReaders
public void Dispose()
{
#if NET40_OR_GREATER
_waitEvent.Dispose();
_outEvent.Dispose();
#endif
}
private void MainLoop()
@@ -45,7 +47,7 @@ namespace Compress.ThreadReaders
break;
}
crc.SlurpBlock(_buffer,0,_size);
crc.SlurpBlock(_buffer, 0, _size);
_outEvent.Set();
}

View File

@@ -10,7 +10,7 @@ using FileInfo = RVIO.FileInfo;
namespace Compress.ZipFile
{
public partial class Zip : ICompress
{
{
private readonly List<ZipLocalFile> _localFiles = new();
private FileInfo? _zipFileInfo;
@@ -33,7 +33,7 @@ namespace Compress.ZipFile
public Zip()
{
CompressUtils.EncodeSetup();
CompressUtils.EncodeSetup();
}
public ZipOpenType ZipOpen { get; private set; }
@@ -56,7 +56,7 @@ namespace Compress.ZipFile
{
return _localFiles[i];
}
public void ZipFileClose()
{
switch (ZipOpen)
@@ -79,7 +79,11 @@ namespace Compress.ZipFile
public void BreakTrrntZip(string filename)
{
_zipFs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
using (BinaryReader zipBr = new BinaryReader(_zipFs,Encoding.UTF8,true))
#if NET20 || NET35 || NET40
using (var zipBr = new BinaryReader(_zipFs,Encoding.UTF8))
#else
using (var zipBr = new BinaryReader(_zipFs,Encoding.UTF8,true))
#endif
{
_zipFs.Position = _zipFs.Length - 22;
byte[] fileComment = zipBr.ReadBytes(22);

View File

@@ -56,7 +56,11 @@ namespace Compress.ZipFile
private ZipReturn EndOfCentralDirRead()
{
#if NET20 || NET35 || NET40
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8);
#else
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
#endif
uint thisSignature = zipBr.ReadUInt32();
if (thisSignature != EndOfCentralDirSignature)
{
@@ -105,7 +109,11 @@ namespace Compress.ZipFile
private void EndOfCentralDirWrite()
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(_zipFs, Encoding.UTF8);
#else
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
#endif
bw.Write(EndOfCentralDirSignature);
bw.Write((ushort)0); // NumberOfThisDisk
bw.Write((ushort)0); // NumberOfThisDiskCenterDir
@@ -122,7 +130,11 @@ namespace Compress.ZipFile
private ZipReturn Zip64EndOfCentralDirRead()
{
#if NET20 || NET35 || NET40
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8);
#else
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
#endif
uint thisSignature = zipBr.ReadUInt32();
if (thisSignature != Zip64EndOfCentralDirSignature)
{
@@ -174,7 +186,11 @@ namespace Compress.ZipFile
private void Zip64EndOfCentralDirWrite()
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(_zipFs, Encoding.UTF8);
#else
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
#endif
bw.Write(Zip64EndOfCentralDirSignature);
bw.Write((ulong)44); // Size of zip64 end of central directory record
bw.Write((ushort)45); // version made by
@@ -191,7 +207,11 @@ namespace Compress.ZipFile
private ZipReturn Zip64EndOfCentralDirectoryLocatorRead()
{
#if NET20 || NET35 || NET40
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8);
#else
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
#endif
uint thisSignature = zipBr.ReadUInt32();
if (thisSignature != Zip64EndOfCentralDirectoryLocator)
{
@@ -217,7 +237,11 @@ namespace Compress.ZipFile
private void Zip64EndOfCentralDirectoryLocatorWrite()
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(_zipFs, Encoding.UTF8);
#else
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
#endif
bw.Write(Zip64EndOfCentralDirectoryLocator);
bw.Write((uint)0); // number of the disk with the start of the zip64 end of central directory
bw.Write(_endOfCentralDir64); // relative offset of the zip64 end of central directory record

View File

@@ -55,7 +55,11 @@ namespace Compress.ZipFile
{
try
{
#if NET20 || NET35 || NET40
using BinaryReader br = new(zipFs, Encoding.UTF8);
#else
using BinaryReader br = new(zipFs, Encoding.UTF8, true);
#endif
uint thisSignature = br.ReadUInt32();
if (thisSignature != CentralDirectoryHeaderSignature)
{
@@ -156,7 +160,7 @@ namespace Compress.ZipFile
switch (_compressionMethod)
{
case 0: // The file is stored (no compression)
case 8: // The file is Deflated
case 9: // Enhanced Deflating using Deflate64(tm)
case 12: // The file is BZIP2 algorithm.
@@ -182,7 +186,11 @@ namespace Compress.ZipFile
internal void CentralDirectoryWrite(Stream crcStream)
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(crcStream, Encoding.UTF8);
#else
using BinaryWriter bw = new(crcStream, Encoding.UTF8, true);
#endif
ZipExtraFieldWrite zefw = new();
SetStatus(LocalFileStatus.Zip64,
@@ -238,7 +246,11 @@ namespace Compress.ZipFile
try
{
#if NET20 || NET35 || NET40
using (BinaryReader br = new(zipFs, Encoding.UTF8))
#else
using (BinaryReader br = new(zipFs, Encoding.UTF8, true))
#endif
{
SetStatus(LocalFileStatus.TrrntZip);
@@ -368,7 +380,11 @@ namespace Compress.ZipFile
try
{
#if NET20 || NET35 || NET40
using BinaryReader br = new(zipFs, Encoding.UTF8);
#else
using BinaryReader br = new(zipFs, Encoding.UTF8, true);
#endif
SetStatus(LocalFileStatus.TrrntZip);
zipFs.Position = (long)RelativeOffsetOfLocalHeader;
@@ -431,7 +447,11 @@ namespace Compress.ZipFile
private void LocalFileHeaderWrite(Stream zipFs)
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(zipFs, Encoding.UTF8);
#else
using BinaryWriter bw = new(zipFs, Encoding.UTF8, true);
#endif
ZipExtraFieldWrite zefw = new();
bool zip64 = zefw.Zip64(UncompressedSize, _compressedSize, RelativeOffsetOfLocalHeader, false,
out uint headerUnCompressedSize, out uint headerCompressedSize,
@@ -505,7 +525,11 @@ namespace Compress.ZipFile
long posNow = zipFs.Position;
zipFs.Seek((long)RelativeOffsetOfLocalHeader + 14, SeekOrigin.Begin);
#if NET20 || NET35 || NET40
using (BinaryWriter bw = new(zipFs, Encoding.UTF8))
#else
using (BinaryWriter bw = new(zipFs, Encoding.UTF8, true))
#endif
{
WriteCRC(bw, CRC);
bw.Write(headerCompressedSize);
@@ -524,7 +548,11 @@ namespace Compress.ZipFile
internal void LocalFileHeaderFake(ulong filePosition, ulong uncompressedSize, ulong compressedSize, byte[] crc32, MemoryStream ms)
{
#if NET20 || NET35 || NET40
using BinaryWriter bw = new(ms, Encoding.UTF8);
#else
using BinaryWriter bw = new(ms, Encoding.UTF8, true);
#endif
RelativeOffsetOfLocalHeader = filePosition;
SetStatus(LocalFileStatus.TrrntZip);
UncompressedSize = uncompressedSize;
@@ -580,7 +608,7 @@ namespace Compress.ZipFile
compressionMethod = _compressionMethod;
readStream = null;
if (zipFs == null)
return ZipReturn.ZipErrorFileNotFound;
@@ -634,11 +662,13 @@ namespace Compress.ZipFile
break;
}
#if NET462_OR_GREATER || NETCOREAPP
case 20:
case 93:
readStream = new ZstdSharp.DecompressionStream(zipFs);
streamSize = UncompressedSize;
break;
#endif
case 98:
{
@@ -675,10 +705,12 @@ namespace Compress.ZipFile
{
writeStream = zipFs;
}
#if NET462_OR_GREATER || NETCOREAPP
else if (compressionMethod == 93)
{
writeStream = new ZstdSharp.CompressionStream(zipFs, 19);
}
#endif
else if (compressionMethod == 8)
{
writeStream = new ZlibBaseStream(zipFs, CompressionMode.Compress, CompressionLevel.BestCompression, ZlibStreamFlavor.DEFLATE, true);
@@ -712,7 +744,11 @@ namespace Compress.ZipFile
private void FixFileForZip64(Stream zipFs, int oldExtraFieldLength, int newExtraFieldLength)
{
long posNow = zipFs.Position;
#if NET20 || NET35 || NET40
using (BinaryWriter bw = new(zipFs, Encoding.UTF8))
#else
using (BinaryWriter bw = new(zipFs, Encoding.UTF8, true))
#endif
{
zipFs.Seek((long)RelativeOffsetOfLocalHeader + 4, SeekOrigin.Begin);
ushort versionNeededToExtract = 45;

View File

@@ -90,7 +90,7 @@ namespace Compress.gZip
ZipFileClose();
return ZipReturn.ZipErrorOpeningFile;
}
catch(Exception)
catch (Exception)
{
ZipFileClose();
return ZipReturn.ZipErrorReadingFile;
@@ -111,7 +111,11 @@ namespace Compress.gZip
private ZipReturn ZipFileReadHeaders()
{
#if NET20 || NET35 || NET40
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8);
#else
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
#endif
byte ID1 = zipBr.ReadByte();
byte ID2 = zipBr.ReadByte();
@@ -316,7 +320,11 @@ namespace Compress.gZip
public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong unCompressedSize, ushort compressionMethod, out Stream stream, TimeStamps dateTime)
{
#if NET20 || NET35 || NET40
using (BinaryWriter zipBw = new(_zipFs, Encoding.UTF8))
#else
using (BinaryWriter zipBw = new(_zipFs, Encoding.UTF8, true))
#endif
{
UnCompressedSize = unCompressedSize;
@@ -422,7 +430,11 @@ namespace Compress.gZip
CompressedSize = (ulong)(_zipFs.Position - dataStartPos);
#if NET20 || NET35 || NET40
using (BinaryWriter zipBw = new(_zipFs, Encoding.UTF8))
#else
using (BinaryWriter zipBw = new(_zipFs, Encoding.UTF8, true))
#endif
{
zipBw.Write(crc32[3]);