Skip to content

Commit

Permalink
fix - use resource submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Nov 27, 2023
1 parent dd91f57 commit b68e821
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
27 changes: 13 additions & 14 deletions src/DotFastLZ.Resource/R.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static class R

// ready zip files
public static readonly ImmutableArray<SourceZip> SourceZipFiles = ImmutableArray.Create(
new SourceZip("canterburycorpus.zip", "canterbury"),
new SourceZip("silesia.zip", "silesia"),
new SourceZip("enwik8.zip", "enwik")
new SourceZip("enwik/enwik8.zip", "enwik/enwik8.txt")
);

public static readonly ImmutableArray<string> SourceFiles = ImmutableArray.Create(
Expand All @@ -65,7 +63,7 @@ public static class R
"silesia/webster",
"silesia/x-ray",
"silesia/xml",
"enwik/enwik8"
"enwik/enwik8.txt"
);


Expand All @@ -79,20 +77,22 @@ public static byte[] ToBytes(string filename)
return buffer;
}

public static string Find(string pathName)
public static string Find(string path)
{
string path = Path.Combine("resources", pathName);
string rpath = Path.Combine(Prefix, path);
//Console.WriteLine(rpath);
for (int i = 0; i < 10; ++i)
{
if (File.Exists(path) || Directory.Exists(path))
if (File.Exists(rpath) || Directory.Exists(rpath))
{
return Path.GetFullPath(path);
return Path.GetFullPath(rpath);
}

path = Path.Combine("..", path);
rpath = Path.Combine("..", rpath);
//Console.WriteLine($"{i} - {rpath}");
}

return Path.GetFullPath(pathName);
return Path.GetFullPath(rpath);
}

public static int ExtractZipFile(string zipFilePath, string destDir)
Expand Down Expand Up @@ -200,16 +200,15 @@ public static void ExtractAll()
// extract source files
foreach (var sourceZip in SourceZipFiles)
{
sourceZip.Extract(Prefix);
sourceZip.Extract("");
}
}

public static void DeleteAll()
{
var path = Find(Prefix);
if (!string.IsNullOrEmpty(path))
foreach (var sourceZip in SourceZipFiles)
{
Directory.Delete(path, true);
sourceZip.Delete();
}
}
}
27 changes: 20 additions & 7 deletions src/DotFastLZ.Resource/SourceZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,38 @@ namespace DotFastLZ.Resource;

public class SourceZip
{
private readonly string _fileName;
private readonly string _extractPath;
private readonly string _path;
private readonly string _deletablePath;

public SourceZip(string fileName, string extractPath)
public SourceZip(string path, string deletablePath)
{
_fileName = fileName;
_extractPath = extractPath;
_path = path;
_deletablePath = deletablePath;
}

public void Extract(string extractRootPath)
{
var zipFilePath = R.Find(_fileName);
var zipFilePath = R.Find(_path);
var directoryName = Path.GetDirectoryName(zipFilePath);
if (null == directoryName)
{
throw new DirectoryNotFoundException($"not found directoryName - {zipFilePath}");
}

var extractPath = Path.Combine(directoryName, extractRootPath, _extractPath);
var extractPath = Path.Combine(directoryName, extractRootPath);
R.ExtractZipFile(zipFilePath, extractPath);
}

public void Delete()
{
var deletePath = R.Find(_deletablePath);
if (Directory.Exists(deletePath))
{
Directory.Delete(deletePath, true);
}
else if (File.Exists(deletePath))
{
File.Delete(deletePath);
}
}
}
8 changes: 4 additions & 4 deletions test/DotFastLZ.Compression.Tests/RoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void TestRefDecompressorLevel1()
Console.WriteLine("Test reference decompressor for Level 1");
foreach (var name in R.SourceFiles)
{
var filename = R.Find(Path.Combine(R.Prefix, name));
var filename = R.Find(name);
bool result = TestHelper.test_ref_decompressor_level1(name, filename);
Assert.That(result, Is.EqualTo(true), $"test_ref_decompressor_level1({name}, {filename})");
}
Expand All @@ -65,7 +65,7 @@ public void TestRefDecompressorLevel2()
Console.WriteLine("Test reference decompressor for Level 2");
foreach (var name in R.SourceFiles)
{
var filename = R.Find(Path.Combine(R.Prefix, name));
var filename = R.Find(name);
bool result = TestHelper.test_ref_decompressor_level2(name, filename);
Assert.That(result, Is.EqualTo(true), $"test_ref_decompressor_level2({name}, {filename})");
}
Expand All @@ -78,7 +78,7 @@ public void TestRoundtripLevel1()
Console.WriteLine("Test round-trip for Level 1");
foreach (var name in R.SourceFiles)
{
var filename = R.Find(Path.Combine(R.Prefix, name));
var filename = R.Find(name);
bool result = TestHelper.test_roundtrip_level1(name, filename);
Assert.That(result, Is.EqualTo(true), $"test_roundtrip_level1({name}, {filename})");
}
Expand All @@ -90,7 +90,7 @@ public void TestRoundtripLevel2()
Console.WriteLine("Test round-trip for Level 2");
foreach (var name in R.SourceFiles)
{
var filename = R.Find(Path.Combine(R.Prefix, name));
var filename = R.Find(name);
var result = TestHelper.test_roundtrip_level2(name, filename);
Assert.That(result, Is.EqualTo(true), $"test_roundtrip_level2({name}, {filename})");
}
Expand Down

0 comments on commit b68e821

Please sign in to comment.