class gz
{
public static void Compress(string filename)
{
if (filename.IndexOf(".gz") >= 1)
{
return;
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[GZIP]compression on file {0}", filename);
FileStream infile;
try
{
infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[infile.Length];
int count = infile.Read(buffer, 0, buffer.Length);
if (count != buffer.Length)
{
infile.Close();
Console.WriteLine("[GZIP]Failed: Unable to read data from file");
return;
}
infile.Close();
FileStream fs = new FileStream(filename + ".gz", FileMode.Create, FileAccess.Write, FileShare.Write);
GZipStream compressedzipStream = new GZipStream(fs, CompressionMode.Compress, true);
Console.WriteLine("[GZIP]Compression");
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
Console.WriteLine("[GZIP]Original size: {0}, Compressed size: {1}", buffer.Length, fs.Length);
fs.Close();
File.Delete(filename);
}
catch (InvalidDataException)
{
Console.WriteLine("[GZIP]Error: The file being read contains invalid data.");
}
catch (FileNotFoundException)
{
Console.WriteLine("[GZIP]Error:The file specified was not found.");
}
catch (ArgumentException)
{
Console.WriteLine("[GZIP]Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
}
catch (PathTooLongException)
{
Console.WriteLine("[GZIP]Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("[GZIP]Error: The specified path is invalid, such as being on an unmapped drive.");
}
catch (IOException)
{
Console.WriteLine("[GZIP]Error: An I/O error occurred while opening the file.");
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("[GZIP]Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("[GZIP]Error: You must provide parameters for GZIP.");
}
finally
{
Console.ForegroundColor = ConsoleColor.White;
}
}
}