Packager/Packager.cs
azures04 c8fabe999e Initial commit with packager implementation
Add AzuresPackager project with .gitignore, solution and project files, core Packager.cs implementation, and sample source files. Implements basic package creation, extraction, and file handling using SharpCompress.
2026-01-26 23:20:34 +01:00

112 lines
3.6 KiB
C#

using System.IO;
using System.Text;
using SharpCompress.Archives;
using SharpCompress.Common;
using SharpCompress.Writers;
using System.Collections.Generic;
using System.Linq;
using System;
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",
};
}
}