Advertisement
fcamuso

Streams - compressione con Brotli e ZIP

Aug 8th, 2021
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.IO.Compression;
  3. using System.IO;
  4. using System.Diagnostics;
  5.  
  6.  
  7. namespace StreamZIP_A
  8. {
  9.   class Program
  10.   {
  11.     static void Main(string[] args)
  12.     {
  13.       //deflate, gzip, Brotli, zip
  14.  
  15.       Stopwatch watch = Stopwatch.StartNew();
  16.  
  17.       //DEFLATE, compressione (4.7 sec)
  18.       using (FileStream origine = File.OpenRead("enwik8.txt"))
  19.       using (FileStream destinazione = File.Create("compressoDeflate.bin"))
  20.       using (DeflateStream compressore = new(destinazione, CompressionMode.Compress))
  21.         origine.CopyTo(compressore);
  22.  
  23.       watch.Stop();
  24.       Console.WriteLine($"Deflate compressione {watch.ElapsedMilliseconds}");
  25.  
  26.       watch.Start();
  27.       //DEFLATE, DEcompressione
  28.       using (FileStream origine = File.OpenRead("compressoDeflate.bin"))
  29.       using (FileStream destinazione = File.Create("DEcompressoDeflate.txt"))
  30.       using (DeflateStream DeCompressore = new (origine, CompressionMode.Decompress))
  31.         DeCompressore.CopyTo(destinazione);
  32.       watch.Stop();
  33.       Console.WriteLine($"Deflate DEcompressione {watch.ElapsedMilliseconds}");
  34.  
  35.  
  36.       //Brotli, compressione
  37.       watch.Start();
  38.       using (FileStream origine = File.OpenRead("enwik8.txt"))
  39.       using (FileStream destinazione = File.Create("compressoBrotli.bin"))
  40.       using (BrotliStream compressore = new(destinazione, CompressionLevel.Fastest))
  41.         origine.CopyTo(compressore);
  42.  
  43.       watch.Stop();
  44.       Console.WriteLine($"Brotli compressione {watch.ElapsedMilliseconds}");
  45.  
  46.  
  47.       watch.Start();
  48.       //Brotli, DEcompressione
  49.       using (FileStream origine = File.OpenRead("compressoBrotli.bin"))
  50.       using (FileStream destinazione = File.Create("DEcompressoBrotli.txt"))
  51.       using (BrotliStream DeCompressore = new(origine, CompressionMode.Decompress))
  52.         DeCompressore.CopyTo(destinazione);
  53.       watch.Stop();
  54.       Console.WriteLine($"Brotlin DEcompressione {watch.ElapsedMilliseconds}");
  55.  
  56.       watch.Start();
  57.       using (FileStream destinazione = File.Create("compressoZIP.zip"))
  58.       using (ZipArchive zip = new(destinazione, ZipArchiveMode.Create))
  59.       {
  60.         zip.CreateEntryFromFile("enwik8.txt", "enwik8.txt");
  61.         zip.CreateEntryFromFile("StreamZIP_A.dll", "StreamZIP_A.dll");
  62.         zip.CreateEntryFromFile("compressoBrotli.bin", "compressoBrotli.bin");
  63.         zip.CreateEntryFromFile("compressoDeflate.bin", "compressoDeflate.bin");
  64.       }
  65.       watch.Stop();
  66.       Console.WriteLine($"ZIP compressione {watch.ElapsedMilliseconds}");
  67.     }
  68.   }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement