Introduces a new Cli.cs file providing a command-line interface for packing and unpacking with AzuresPackager. Updates project metadata in AzuresPackager.csproj, including package version, output type, and dependency version bump for SharpCompress. Also updates publish.ps1 to use the new version and skip duplicate package uploads.
108 lines
3.5 KiB
C#
108 lines
3.5 KiB
C#
using System.Text;
|
|
using SharpCompress.Archives;
|
|
using SharpCompress.Common;
|
|
using SharpCompress.Writers;
|
|
|
|
namespace AzuresPackager;
|
|
|
|
public class AzuresPackage : IDisposable {
|
|
private readonly byte[] _data;
|
|
private readonly byte _key;
|
|
|
|
public AzuresPackage(string filePath, byte key) {
|
|
_key = key;
|
|
_data = File.ReadAllBytes(filePath);
|
|
Decode();
|
|
}
|
|
|
|
private void Decode() {
|
|
for (int i = 0; i < _data.Length; i++) {
|
|
_data[i] = (byte)(_data[i] ^ _key);
|
|
}
|
|
}
|
|
|
|
public List<string> ListEntries() {
|
|
using var ms = new MemoryStream(_data);
|
|
using var archive = ArchiveFactory.Open(ms);
|
|
return archive.Entries.Select(e => e.Key!).ToList();
|
|
}
|
|
|
|
public string GetFileContent(string internalPath) {
|
|
using var ms = new MemoryStream(_data);
|
|
using var archive = ArchiveFactory.Open(ms);
|
|
var entry = archive.Entries.FirstOrDefault(e => e.Key == internalPath);
|
|
|
|
if (entry == null || entry.IsDirectory) return string.Empty;
|
|
|
|
using var entryStream = entry.OpenEntryStream();
|
|
using var reader = new StreamReader(entryStream, Encoding.UTF8);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
public byte[] GetFileBytes(string internalPath) {
|
|
using var ms = new MemoryStream(_data);
|
|
using var archive = ArchiveFactory.Open(ms);
|
|
var entry = archive.Entries.FirstOrDefault(e => e.Key == internalPath);
|
|
|
|
if (entry == null) return Array.Empty<byte>();
|
|
|
|
using var entryStream = entry.OpenEntryStream();
|
|
using var outMs = new MemoryStream();
|
|
entryStream.CopyTo(outMs);
|
|
return outMs.ToArray();
|
|
}
|
|
|
|
public void Unpack(string targetDirectory) {
|
|
if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory);
|
|
|
|
using var ms = new MemoryStream(_data);
|
|
using var archive = ArchiveFactory.Open(ms);
|
|
foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) {
|
|
entry.WriteToDirectory(targetDirectory, new ExtractionOptions {
|
|
ExtractFullPath = true,
|
|
Overwrite = true
|
|
});
|
|
}
|
|
}
|
|
|
|
public void Dispose() {
|
|
Array.Clear(_data, 0, _data.Length);
|
|
}
|
|
}
|
|
|
|
public static class Packager {
|
|
public static void Pack(string sourceFolder, string outputPath, byte key) {
|
|
using var memoryStream = new MemoryStream();
|
|
var writerOptions = new WriterOptions(CompressionType.LZMA);
|
|
|
|
using (var writer = WriterFactory.Open(memoryStream, ArchiveType.Zip, writerOptions)) {
|
|
writer.WriteAll(sourceFolder, "*", SearchOption.AllDirectories);
|
|
}
|
|
|
|
byte[] data = memoryStream.ToArray();
|
|
|
|
for (int i = 0; i < data.Length; i++) {
|
|
data[i] = (byte)(data[i] ^ key);
|
|
}
|
|
|
|
File.WriteAllBytes(outputPath, data);
|
|
}
|
|
}
|
|
|
|
public static class MimeHelper {
|
|
public static string GetMimeType(string path) {
|
|
string extension = Path.GetExtension(path).ToLower();
|
|
return extension switch
|
|
{
|
|
".html" or ".htm" => "text/html",
|
|
".css" => "text/css",
|
|
".js" => "text/javascript",
|
|
".png" => "image/png",
|
|
".jpg" or ".jpeg" => "image/jpeg",
|
|
".svg" => "image/svg+xml",
|
|
".woff2" => "font/woff2",
|
|
".json" => "application/json",
|
|
_ => "application/octet-stream",
|
|
};
|
|
}
|
|
} |