Skip to content

Commit

Permalink
Actually implement the compress method
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jul 7, 2024
1 parent 6857e20 commit 9a77fe6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/Helpers/ZStdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,21 @@ public static SpanOwner<byte> Decompress(string file)

public static void Compress(string file, string output, bool useDictionaries)
{
using SpanOwner<byte> compressed = Compress(file, useDictionaries);
Span<byte> compressed = Compress(file, useDictionaries);
using FileStream fs = File.Create(output);
fs.Write(compressed.Span);
fs.Write(compressed);
}

public static SpanOwner<byte> Compress(string file, bool useDictionaries)
public static Span<byte> Compress(string file, bool useDictionaries)
{
using FileStream fs = File.OpenRead(file);
int size = Convert.ToInt32(fs.Length);
using SpanOwner<byte> buffer = SpanOwner<byte>.Allocate(size);
fs.Read(buffer.Span);

size = Zstd.GetDecompressedSize(buffer.Span);
SpanOwner<byte> decompressed = SpanOwner<byte>.Allocate(size);
Totk.Zstd.Decompress(buffer.Span, decompressed.Span);
return decompressed;
SpanOwner<byte> compressed = SpanOwner<byte>.Allocate(size);
size = Totk.Zstd.Compress(buffer.Span, compressed.Span, file.GetDictioanryId(useDictionaries));
return compressed.Span[..size];
}

public static void DecompressFolder(string path, string output, bool recursive, Action<int>? setCount = null, Action<int>? updateCount = null)
Expand Down Expand Up @@ -83,12 +82,12 @@ public static void CompressFolder(string path, string output, bool recursive, Ac
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
using SpanOwner<byte> data = Compress(file, useDictionaries);
Span<byte> compresses = Compress(file, useDictionaries);

string outputFile = Path.Combine(output, Path.GetRelativePath(path, $"{file}.zs"));
Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!);
using FileStream fs = File.Create(outputFile);
fs.Write(data.Span);
fs.Write(compresses);

updateCount?.Invoke(i + 1);
}
Expand Down

0 comments on commit 9a77fe6

Please sign in to comment.