forked from revenz/FileFlows
/
Gzipper.cs
145 lines (130 loc) · 5.36 KB
/
Gzipper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.IO.Compression;
using System.Text;
namespace FileFlows.Server.Helpers;
/// <summary>
/// Helper class for gzip functions
/// </summary>
public class Gzipper
{
/// <summary>
/// Compresses a file
/// </summary>
/// <param name="inputFile">the input file to gzip</param>
/// <param name="outputFile">the output file to save the new file to</param>
/// <param name="deleteInput">if the input file should be deleted afterwards</param>
/// <returns>true if successful</returns>
public static bool CompressFile(string inputFile, string outputFile, bool deleteInput = false)
{
try
{
if (File.Exists(outputFile))
File.Delete(outputFile);
using (FileStream originalFileStream = FileOpenHelper.OpenRead_NoLocks(inputFile))
{
using FileStream compressedFileStream = File.Create(outputFile);
using var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress);
originalFileStream.CopyTo(compressor);
}
if(deleteInput)
File.Delete(inputFile);
return true;
}
catch (Exception ex)
{
Shared.Logger.Instance?.WLog("Failed compressing file: " + ex.Message);
return false;
}
}
/// <summary>
/// Decompresses a file
/// </summary>
/// <param name="inputFile">the input file to gzip</param>
/// <param name="outputFile">the output file to save the new file to</param>
/// <param name="deleteInput">if the input file should be deleted afterwards</param>
/// <returns>true if successful</returns>
public static bool DecompressFile(string inputFile, string outputFile, bool deleteInput = false)
{
try
{
if (File.Exists(outputFile))
File.Delete(outputFile);
using FileStream compressedFileStream = FileOpenHelper.OpenRead_NoLocks(inputFile);
using FileStream outputFileStream = File.Create(outputFile);
using var decompressor = new GZipStream(compressedFileStream, CompressionMode.Decompress);
decompressor.CopyTo(outputFileStream);
if(deleteInput)
File.Delete(inputFile);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Compresses a string and saves it to a specified file
/// </summary>
/// <param name="file">The file where to save the compressed contents</param>
/// <param name="contents">the contents to compress</param>
public static void CompressToFile(string file, string contents)
{
if (File.Exists(file))
File.Delete(file);
var bytes = Encoding.UTF8.GetBytes(contents);
using var input = new MemoryStream(bytes);
using FileStream compressedFileStream = File.Create(file);
using var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress);
input.CopyTo(compressor);
}
/// <summary>
/// Decompresses a file and returns the string contents
/// </summary>
/// <param name="inputFile">the file to decompress</param>
/// <param name="lines">an optional number of lines to grab, 0 to grab all</param>
/// <returns>the decompresses file contents</returns>
public static string DecompressFileToString(string inputFile, int lines = 0, int bytes = 0)
{
using FileStream compressedFileStream = FileOpenHelper.OpenRead_NoLocks(inputFile);
using MemoryStream outputStream = new MemoryStream();
using var decompressor = new GZipStream(compressedFileStream, CompressionMode.Decompress);
if (lines > 0)
{
decompressor.CopyTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin); // Reset the stream position to start
using (StreamReader reader = new StreamReader(outputStream, Encoding.UTF8))
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < lines; i++)
{
var line = reader.ReadLine();
if (line == null)
break;
result.AppendLine(line);
}
return result.ToString();
}
}
else if (bytes > 0)
{
const int bufferSize = 1024; // Adjust the buffer size as needed
byte[] buffer = new byte[bufferSize];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = decompressor.Read(buffer, 0, Math.Min(bufferSize, bytes - totalBytesRead))) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
if (totalBytesRead >= bytes)
break; // Break if the specified number of bytes has been reached
}
outputStream.Seek(0, SeekOrigin.Begin); // Reset the stream position to start
return Encoding.UTF8.GetString(outputStream.ToArray());
}
else
{
decompressor.CopyTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin); // Reset the stream position to start
return Encoding.UTF8.GetString(outputStream.ToArray());
}
}
}