Advertisement
Fhernd

UsoGZipStream.cs

Jul 23rd, 2015
20,959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.IO;
  5. using System.IO.Compression;
  6.  
  7. namespace Receta.CSharp.R0523
  8. {
  9.     public class UsoGZipStream
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             // Valida la entrada del usuario:
  16.             if (args.Length != 1)
  17.             {
  18.                 Console.WriteLine("USO: UsoGZipStream.exe [NombreArchivo]");
  19.                 Console.WriteLine (Environment.NewLine);
  20.                 return;
  21.             }
  22.            
  23.             try
  24.             {
  25.                 // Nombre de archivo a comprimir:
  26.                 string nombreArchivo = args[0];
  27.                
  28.                 // Validación existencia archivo:
  29.                 if (!File.Exists(nombreArchivo))
  30.                 {
  31.                     Console.WriteLine ("El archivo no existe. Intente de nuevo.");
  32.                     Console.WriteLine (Environment.NewLine);
  33.                     return;
  34.                 }
  35.                
  36.                 using (FileStream fs = File.OpenRead(nombreArchivo))
  37.                 {
  38.                     Console.WriteLine ("Tamaño del archivo {0}: {1} bytes.", nombreArchivo, fs.Length.ToString());
  39.                     ComprimirArchivo(fs);
  40.                 }
  41.             }
  42.             catch(Exception ex)
  43.             {
  44.                 Console.WriteLine ("Error: El archivo no pudo ser comprimido: {0}", ex.Message);
  45.             }
  46.            
  47.             Console.WriteLine(Environment.NewLine);
  48.         }
  49.        
  50.         private static void ComprimirArchivo(FileStream archivo)
  51.         {
  52.             byte[] bytes = new byte[archivo.Length];
  53.             archivo.Read(bytes, 0, (int)archivo.Length);
  54.            
  55.             // Comprime el archivo:
  56.             using(FileStream archivoComprimido = new FileStream(String.Format("{0}.gz", archivo.Name), FileMode.Create))
  57.             {
  58.                 using (GZipStream compresor = new GZipStream(archivoComprimido, CompressionMode.Compress, false))
  59.                 {
  60.                     compresor.Write(bytes, 0, bytes.Length);
  61.                 }
  62.             }
  63.            
  64.             // Muestra tamaño del archivo recién comprimido:
  65.             using(FileStream archivoComprimido = File.OpenRead(String.Format("{0}.gz", archivo.Name)))
  66.             {
  67.                 Console.WriteLine("Tamaño del archivo {0}: {1} bytes.", Path.GetFileName(archivoComprimido.Name), archivoComprimido.Length);
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement