Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compress and decompress files using the Deflate algorithm
/*
* Title: Deflate Tool
* Purpose: Compress and decompress files using the Deflate algorithm
* Author: Osanda Malith Jayathissa (@OsandaMalith)
* Website: https://OsandaMalith.com
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
namespace compress2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("[~] Deflate Tool - @OsandaMalith\n");
if (args.Length != 3)
{
Console.WriteLine("Usage: {0} input.dat output.dat -c or -d", System.AppDomain.CurrentDomain.FriendlyName);
System.Environment.Exit(-1);
}
DeflateStream deflate = null;
FileStream sink = null;
FileStream source = new FileStream(Environment.GetCommandLineArgs()[1], FileMode.Open);
if (Environment.GetCommandLineArgs()[3].Substring(1, 1).Equals("d"))
{
deflate = new DeflateStream(source, CompressionMode.Decompress);
sink = new FileStream(Environment.GetCommandLineArgs()[2], FileMode.Create);
byte[] buffer = new byte[source.Length];
buffer = new byte[source.Length];
deflate.Read(buffer, 0, buffer.Length);
sink.Write(buffer, 0, buffer.Length);
sink.Close();
}
else if (Environment.GetCommandLineArgs()[3].Substring(1, 1).Equals("c"))
{
deflate = new DeflateStream(source, CompressionMode.Compress);
sink = new FileStream(Environment.GetCommandLineArgs()[2], FileMode.Create);
byte[] buffer = new byte[source.Length];
source.Read(buffer, 0, buffer.Length);
deflate.Write(buffer, 0, buffer.Length);
sink.Write(buffer, 0, buffer.Length);
sink.Close();
}
else
{
Console.WriteLine("Enter an option -c compress or -d decompress");
System.Environment.Exit(-1);
}
source.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment