Advertisement
tolikpunkoff

Unpack gzip archive

May 5th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.IO.Compression;
  6.  
  7. namespace tmpAnalyseRP5
  8. {
  9.     public class Ungzip
  10.     {
  11.         public string ErrorMessage { get; private set; }
  12.         public bool IsGZip(string filename)
  13.         {
  14.             byte[] buf = null;
  15.             try
  16.             {
  17.                 buf = File.ReadAllBytes(filename);
  18.             }
  19.             catch
  20.             {
  21.                 return false;
  22.             }
  23.  
  24.             if (buf.Length < 4) return false;
  25.  
  26.             if ((buf[0] == 0x1F) && (buf[1] == 0x8B) &&
  27.                 (buf[2] == 0x08) && (buf[3] == 0x00))
  28.                 return true;
  29.  
  30.             return false;
  31.         }
  32.  
  33.         public string GetUnpackedFilename(string fileName)
  34.         {
  35.             FileInfo fi = new FileInfo(fileName);
  36.             string unpackedFile = fileName.Substring(0, fileName.Length - fi.Extension.Length);
  37.             return unpackedFile;
  38.         }
  39.  
  40.         public bool Unpack(string originalFile, string unpackedFile)
  41.         {            
  42.             GZipStream gzip = null;
  43.             FileStream readStream = null;
  44.             FileStream writeStream = null;
  45.  
  46.             try
  47.             {
  48.                 readStream = new FileStream(originalFile, FileMode.Open);              
  49.                 writeStream = new FileStream(unpackedFile, FileMode.Create);        
  50.                 gzip = new GZipStream(readStream,CompressionMode.Decompress);
  51.  
  52.                 int size = 1024; //размер буфера для обмена между потоками
  53.                 byte[] unpackbuf = new byte[size]; //буфер                
  54.                 int count = 0; //для хранения фактически прочитанных байт
  55.                
  56.                 //пишем распакованные данные по кускам
  57.                 do
  58.                 {
  59.                     count = gzip.Read(unpackbuf, 0, size); //читаем кусками размером size
  60.                     if (count > 0) //если данные есть
  61.                     {                        
  62.                         writeStream.Write(unpackbuf, 0, count); //пишем фактически
  63.                         //прочитанное кол-во байт
  64.                     }
  65.                 } while (count > 0);                
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.                 if (readStream != null) readStream.Close();
  70.                 if (writeStream != null) writeStream.Close();
  71.                 if (gzip != null) gzip.Close();
  72.                 ErrorMessage = ex.Message;
  73.  
  74.                 return false;
  75.             }
  76.            
  77.             gzip.Close();
  78.             readStream.Close();
  79.             writeStream.Close();
  80.             return true;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement