-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (47 loc) · 1.74 KB
/
Program.cs
File metadata and controls
56 lines (47 loc) · 1.74 KB
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
using System.IO.Compression;
using System.Runtime.Versioning;
namespace CompGIFConverter
{
public class Program
{
private static int ConvertedGIFs;
private static int FailedGIFS;
public static void Main()
{
// Loop through every file with the .compgif extension in the current folder, and attempt to create a gif from it.
foreach (var filepath in Directory.EnumerateFiles(Environment.CurrentDirectory, "*.compgif"))
TryCreateGif(filepath);
string failedGifText = FailedGIFS > 0 ? $"\n{FailedGIFS} Gifs failed to convert!": string.Empty;
Console.WriteLine($"{ConvertedGIFs} GIFs converted!{failedGifText}\nPress any key to exit!");
Console.ReadKey(true);
}
public static void TryCreateGif(string inputFilePath)
{
if (!File.Exists(inputFilePath))
{
FailedGIFS++;
return;
}
try
{
// Read the data from the file.
var compressedData = File.ReadAllBytes(inputFilePath);
// Create two streams, one from the data, and one to be written to after decompression.
using MemoryStream compressedStream = new(compressedData);
using MemoryStream decompressedStream = new();
// Decompress the data stream, copy it to the memory stream and get the decompressed data back.
using GZipStream gZipStream = new(compressedStream, CompressionMode.Decompress);
gZipStream.CopyTo(decompressedStream);
var decompressedData = decompressedStream.GetBuffer();
// Create a suitable new file name, and save the data to a new file.
var outputFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(inputFilePath) + "GIF.gif" ;
File.WriteAllBytes(outputFilePath, decompressedData);
ConvertedGIFs++;
}
catch
{
FailedGIFS++;
}
}
}
}