1
0
mirror of synced 2024-11-30 14:14:29 +01:00
TaikoSoundEditor/GZ.cs

91 lines
3.1 KiB
C#
Raw Normal View History

2023-07-17 10:20:00 +02:00
using System;
using System.Collections.Generic;
2023-07-17 16:26:25 +02:00
using System.Diagnostics;
2023-07-17 10:20:00 +02:00
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-07-17 16:26:25 +02:00
using static TaikoSoundEditor.TJA;
2023-07-17 10:20:00 +02:00
namespace TaikoSoundEditor
{
internal class GZ
{
public static string DecompressString(string gzPath)
{
2023-07-19 07:10:25 +02:00
Logger.Info("GZ Decompressing string");
2023-07-17 10:20:00 +02:00
using FileStream originalFileStream = File.OpenRead(gzPath);
using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress);
using StreamReader reader = new StreamReader(decompressionStream);
return reader.ReadToEnd();
}
public static byte[] DecompressBytes(string gzPath)
{
2023-07-19 07:10:25 +02:00
Logger.Info("GZ Decompressing bytes");
2023-07-17 10:20:00 +02:00
using FileStream originalFileStream = File.OpenRead(gzPath);
using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress);
using MemoryStream ms = new MemoryStream();
decompressionStream.CopyTo(ms);
return ms.ToArray();
}
public static byte[] CompressToBytes(string content)
{
2023-07-19 07:10:25 +02:00
Logger.Info("GZ Compressing bytes");
2023-07-17 10:20:00 +02:00
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(content);
using var ostream = new MemoryStream();
using (var compressionStream = new GZipStream(ostream, CompressionMode.Compress))
{
stream.CopyTo(compressionStream);
}
return ostream.ToArray();
}
2023-07-17 16:26:25 +02:00
public static string CompressToFile(string fileName, string content)
2023-07-17 10:20:00 +02:00
{
2023-07-19 07:10:25 +02:00
Logger.Info("GZ Compressing file");
2023-07-17 16:26:25 +02:00
var tmp = "~ztmp";
if (!Directory.Exists(tmp))
Directory.CreateDirectory(tmp);
var fn = Path.GetFileNameWithoutExtension(fileName);
fn = Path.Combine(tmp, fn);
File.WriteAllText(fn, content);
fileName = Path.GetFullPath(fileName);
var p = new Process();
p.StartInfo.FileName = Path.GetFullPath(@"Tools\7z\7za.exe");
p.StartInfo.ArgumentList.Add("a");
p.StartInfo.ArgumentList.Add("-tgzip");
p.StartInfo.ArgumentList.Add(fileName);
p.StartInfo.ArgumentList.Add(Path.GetFullPath(fn));
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
foreach (var a in p.StartInfo.ArgumentList)
Debug.WriteLine(a);
p.Start();
p.WaitForExit();
int exitCode = p.ExitCode;
string stderr = p.StandardError.ReadToEnd();
string stdout = p.StandardError.ReadToEnd();
if (exitCode != 0)
throw new Exception($"Process 7za failed with exit code {exitCode}:\n" + stderr);
return stdout;
2023-07-17 10:20:00 +02:00
}
}
}