Skip to content

Commit 0a50386

Browse files
committed
Using Constants class differently
1 parent b9fc680 commit 0a50386

25 files changed

Lines changed: 70 additions & 138 deletions

src/SharpCompress/Archives/ArchiveFactory.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,14 @@ private static T FindFactory<T>(Stream stream)
166166
);
167167
}
168168

169-
public static bool IsArchive(
170-
string filePath,
171-
out ArchiveType? type,
172-
int bufferSize = ReaderOptions.DefaultBufferSize
173-
)
169+
public static bool IsArchive(string filePath, out ArchiveType? type)
174170
{
175171
filePath.NotNullOrEmpty(nameof(filePath));
176172
using Stream s = File.OpenRead(filePath);
177-
return IsArchive(s, out type, bufferSize);
173+
return IsArchive(s, out type);
178174
}
179175

180-
public static bool IsArchive(
181-
Stream stream,
182-
out ArchiveType? type,
183-
int bufferSize = ReaderOptions.DefaultBufferSize
184-
)
176+
public static bool IsArchive(Stream stream, out ArchiveType? type)
185177
{
186178
type = null;
187179
stream.NotNull(nameof(stream));

src/SharpCompress/Archives/AutoArchiveFactory.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ class AutoArchiveFactory : IArchiveFactory
1414

1515
public IEnumerable<string> GetSupportedExtensions() => throw new NotSupportedException();
1616

17-
public bool IsArchive(
18-
Stream stream,
19-
string? password = null,
20-
int bufferSize = ReaderOptions.DefaultBufferSize
21-
) => throw new NotSupportedException();
17+
public bool IsArchive(Stream stream, string? password = null) =>
18+
throw new NotSupportedException();
2219

2320
public FileInfo? GetFilePart(int index, FileInfo part1) => throw new NotSupportedException();
2421

src/SharpCompress/Archives/IArchiveEntryExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace SharpCompress.Archives;
99

1010
public static class IArchiveEntryExtensions
1111
{
12-
private const int BufferSize = 81920;
13-
1412
/// <param name="archiveEntry">The archive entry to extract.</param>
1513
extension(IArchiveEntry archiveEntry)
1614
{
@@ -28,7 +26,7 @@ public void WriteTo(Stream streamToWriteTo, IProgress<ProgressReport>? progress
2826

2927
using var entryStream = archiveEntry.OpenEntryStream();
3028
var sourceStream = WrapWithProgress(entryStream, archiveEntry, progress);
31-
sourceStream.CopyTo(streamToWriteTo, BufferSize);
29+
sourceStream.CopyTo(streamToWriteTo, Constants.BufferSize);
3230
}
3331

3432
/// <summary>
@@ -51,7 +49,7 @@ public async Task WriteToAsync(
5149
using var entryStream = await archiveEntry.OpenEntryStreamAsync(cancellationToken);
5250
var sourceStream = WrapWithProgress(entryStream, archiveEntry, progress);
5351
await sourceStream
54-
.CopyToAsync(streamToWriteTo, BufferSize, cancellationToken)
52+
.CopyToAsync(streamToWriteTo, Constants.BufferSize, cancellationToken)
5553
.ConfigureAwait(false);
5654
}
5755
}

src/SharpCompress/Archives/Tar/TarArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ var header in TarHeaderFactory.ReadHeader(
180180
using (var entryStream = entry.OpenEntryStream())
181181
{
182182
using var memoryStream = new MemoryStream();
183-
entryStream.CopyTo(memoryStream);
183+
entryStream.CopyTo(memoryStream, Constants.BufferSize);
184184
memoryStream.Position = 0;
185185
var bytes = memoryStream.ToArray();
186186

src/SharpCompress/Archives/Zip/ZipArchive.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,38 +124,27 @@ public static ZipArchive Open(Stream stream, ReaderOptions? readerOptions = null
124124
);
125125
}
126126

127-
public static bool IsZipFile(
128-
string filePath,
129-
string? password = null,
130-
int bufferSize = ReaderOptions.DefaultBufferSize
131-
) => IsZipFile(new FileInfo(filePath), password, bufferSize);
132-
133-
public static bool IsZipFile(
134-
FileInfo fileInfo,
135-
string? password = null,
136-
int bufferSize = ReaderOptions.DefaultBufferSize
137-
)
127+
public static bool IsZipFile(string filePath, string? password = null) =>
128+
IsZipFile(new FileInfo(filePath), password);
129+
130+
public static bool IsZipFile(FileInfo fileInfo, string? password = null)
138131
{
139132
if (!fileInfo.Exists)
140133
{
141134
return false;
142135
}
143136
using Stream stream = fileInfo.OpenRead();
144-
return IsZipFile(stream, password, bufferSize);
137+
return IsZipFile(stream, password);
145138
}
146139

147-
public static bool IsZipFile(
148-
Stream stream,
149-
string? password = null,
150-
int bufferSize = ReaderOptions.DefaultBufferSize
151-
)
140+
public static bool IsZipFile(Stream stream, string? password = null)
152141
{
153142
var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null);
154143
try
155144
{
156145
if (stream is not SharpCompressStream)
157146
{
158-
stream = new SharpCompressStream(stream, bufferSize: bufferSize);
147+
stream = new SharpCompressStream(stream, bufferSize: Constants.BufferSize);
159148
}
160149

161150
var header = headerFactory
@@ -177,18 +166,14 @@ public static bool IsZipFile(
177166
}
178167
}
179168

180-
public static bool IsZipMulti(
181-
Stream stream,
182-
string? password = null,
183-
int bufferSize = ReaderOptions.DefaultBufferSize
184-
)
169+
public static bool IsZipMulti(Stream stream, string? password = null)
185170
{
186171
var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null);
187172
try
188173
{
189174
if (stream is not SharpCompressStream)
190175
{
191-
stream = new SharpCompressStream(stream, bufferSize: bufferSize);
176+
stream = new SharpCompressStream(stream, bufferSize: Constants.BufferSize);
192177
}
193178

194179
var header = headerFactory
@@ -229,7 +214,7 @@ protected override IEnumerable<ZipVolume> LoadVolumes(SourceStream stream)
229214
if (streams.Count() > 1) //test part 2 - true = multipart not split
230215
{
231216
streams[1].Position += 4; //skip the POST_DATA_DESCRIPTOR to prevent an exception
232-
var isZip = IsZipFile(streams[1], ReaderOptions.Password, ReaderOptions.BufferSize);
217+
var isZip = IsZipFile(streams[1], ReaderOptions.Password);
233218
streams[1].Position -= 4;
234219
if (isZip)
235220
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SharpCompress.Common;
2+
3+
public static class Constants
4+
{
5+
/// <summary>
6+
/// The default buffer size for stream operations, matching .NET's Stream.CopyTo default of 81920 bytes.
7+
/// This can be modified globally at runtime.
8+
/// </summary>
9+
public static int BufferSize = 81920;
10+
}

src/SharpCompress/Factories/AceFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public override IEnumerable<string> GetSupportedExtensions()
2222
yield return "ace";
2323
}
2424

25-
public override bool IsArchive(
26-
Stream stream,
27-
string? password = null,
28-
int bufferSize = ReaderOptions.DefaultBufferSize
29-
)
25+
public override bool IsArchive(Stream stream, string? password = null)
3026
{
3127
return AceHeader.IsArchive(stream);
3228
}

src/SharpCompress/Factories/ArcFactory.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ public override IEnumerable<string> GetSupportedExtensions()
2323
yield return "arc";
2424
}
2525

26-
public override bool IsArchive(
27-
Stream stream,
28-
string? password = null,
29-
int bufferSize = ReaderOptions.DefaultBufferSize
30-
)
26+
public override bool IsArchive(Stream stream, string? password = null)
3127
{
3228
//You may have to use some(paranoid) checks to ensure that you actually are
33-
//processing an ARC file, since other archivers also adopted the idea of putting
34-
//a 01Ah byte at offset 0, namely the Hyper archiver. To check if you have a
29+
//processing an ARC file, since other archivers also adopted to the idea of putting
30+
//a 01Ah byte at offset 0, namely: Hyper archiver. To check if you have a
3531
//Hyper - archive, check the next two bytes for "HP" or "ST"(or look below for
36-
//"HYP").Also the ZOO archiver also does put a 01Ah at the start of the file,
37-
//see the ZOO entry below.
32+
//"HYP").Also, ZOO archiver also does put a 01Ah at the start of the file,
33+
//see: ZOO entry below.
3834
var bytes = new byte[2];
3935
stream.Read(bytes, 0, 2);
4036
return bytes[0] == 0x1A && bytes[1] < 10; //rather thin, but this is all we have

src/SharpCompress/Factories/ArjFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public override IEnumerable<string> GetSupportedExtensions()
2222
yield return "arj";
2323
}
2424

25-
public override bool IsArchive(
26-
Stream stream,
27-
string? password = null,
28-
int bufferSize = ReaderOptions.DefaultBufferSize
29-
)
25+
public override bool IsArchive(Stream stream, string? password = null)
3026
{
3127
return ArjHeader.IsArchive(stream);
3228
}

src/SharpCompress/Factories/Factory.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ public static void RegisterFactory(Factory factory)
5151
public abstract IEnumerable<string> GetSupportedExtensions();
5252

5353
/// <inheritdoc/>
54-
public abstract bool IsArchive(
55-
Stream stream,
56-
string? password = null,
57-
int bufferSize = ReaderOptions.DefaultBufferSize
58-
);
54+
public abstract bool IsArchive(Stream stream, string? password = null);
5955

6056
/// <inheritdoc/>
6157
public virtual FileInfo? GetFilePart(int index, FileInfo part1) => null;
@@ -82,7 +78,7 @@ out IReader? reader
8278
{
8379
long pos = ((IStreamStack)stream).GetPosition();
8480

85-
if (IsArchive(stream, options.Password, options.BufferSize))
81+
if (IsArchive(stream, options.Password))
8682
{
8783
((IStreamStack)stream).StackSeek(pos);
8884
reader = readerFactory.OpenReader(stream, options);

0 commit comments

Comments
 (0)