Skip to content

Commit

Permalink
Added extraction feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed May 23, 2024
1 parent 2f2580b commit 082879b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
34 changes: 34 additions & 0 deletions chibiar/chibiar.core/Archiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,34 @@ internal bool AddOrUpdate(
return isCreatedArchive;
}

internal void Extract(
string archiveFilePath,
string[] objectNames,
bool isDryrun)
{
if (!isDryrun || File.Exists(archiveFilePath))
{
Parallel.ForEach(objectNames,
objectName =>
{
using var archive = ZipFile.Open(
archiveFilePath,
ZipArchiveMode.Read,
Encoding.UTF8);
using var inputStream = archive.GetEntry(objectName)!.Open();
if (!isDryrun)
{
using var outputStream = StreamUtilities.OpenStream(objectName, true);
inputStream.CopyTo(outputStream);
outputStream.Flush();
}
});
}
}

public void Archive(CliOptions options)
{
switch (options.Mode)
Expand All @@ -233,6 +261,12 @@ public void Archive(CliOptions options)
this.logger.Information($"creating {Path.GetFileName(options.ArchiveFilePath)}");
}
break;
case ArchiveModes.Extract:
this.Extract(
options.ArchiveFilePath,
options.ObjectFilePaths.ToArray(),
options.IsDryRun);
break;
default:
throw new NotImplementedException();
}
Expand Down
7 changes: 6 additions & 1 deletion chibiar/chibiar.core/Cli/CliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ArchiveModes
{
Nothing,
AddOrUpdate,
Extract,
Delete,
List,
}
Expand Down Expand Up @@ -60,6 +61,9 @@ public static CliOptions Parse(string[] args)
case 'u':
options.Mode = ArchiveModes.AddOrUpdate;
break;
case 'x':
options.Mode = ArchiveModes.Extract;
break;
case 'd':
options.Mode = ArchiveModes.Delete;
break;
Expand Down Expand Up @@ -134,8 +138,9 @@ public static CliOptions Parse(string[] args)
public static void WriteUsage(TextWriter tw)
{
tw.WriteLine(" -r, -u Add or update object files into the archive");
tw.WriteLine(" -c Add object files into the archive silently");
tw.WriteLine(" -c Create archive file silently");
tw.WriteLine(" -s Add symbol table (Always enabled)");
tw.WriteLine(" -x Extract object files from the archive");
tw.WriteLine(" -d Delete object files from the archive");
tw.WriteLine(" -t List object files in the archive");
tw.WriteLine(" --log <level> Log level [debug|trace|information|warning|error|silent] (defaulted: warning)");
Expand Down

0 comments on commit 082879b

Please sign in to comment.