Read() public method

Reads uncompressed data into an array of bytes
public Read ( byte buffer, int offset, int count ) : int
buffer byte /// The buffer to read uncompressed data into ///
offset int /// The offset indicating where the data should be placed ///
count int /// The number of uncompressed bytes to be read ///
return int
        public byte[] Decompress(byte[] data)
        {
            using (MemoryStream inputStream = new MemoryStream())
            {
                inputStream.Write(data, 0, data.Length);
                inputStream.Position = 0;

                using (Stream decompressStream = new GZipInputStream(inputStream))
                {
                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        while (true)
                        {
                            int size = decompressStream.Read(buffer, 0, buffer.Length);
                            if (size > 0)
                            {
                                outputStream.Write(buffer, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        decompressStream.Close();
                        return outputStream.ToArray();
                    }
                }
            }
        }
Beispiel #2
0
        public static byte[] GetDecompressedBytes(this WWW www)
        {
            #if UNITY_STANDALONE_WIN || UNITY_WEBGL
            string contentEncoding;
            if (www.responseHeaders == null ||
                !www.responseHeaders.TryGetValue(ContentEncodingHeaderName, out contentEncoding) ||
                !contentEncoding.Equals(GzipContentEncodingValue, StringComparison.OrdinalIgnoreCase))
            {
                return www.bytes;
            }

            byte[] buffer = new byte[4096];
            using (var stream = new MemoryStream(www.bytes))
            using (var gzip = new GZipInputStream(stream))
            using (var outMs = new MemoryStream(www.bytes.Length))
            {
                int bytesRead = 0;
                while ((bytesRead = gzip.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outMs.Write(buffer,0, bytesRead);
                }
                return outMs.ToArray();
            }

            #else
            return www.bytes;
            #endif
        }
		public void TestGZip()
		{
			MemoryStream ms = new MemoryStream();
			GZipOutputStream outStream = new GZipOutputStream(ms);

			byte[] buf = new byte[100000];
			System.Random rnd = new Random();
			rnd.NextBytes(buf);

			outStream.Write(buf, 0, buf.Length);
			outStream.Flush();
			outStream.Finish();

			ms.Seek(0, SeekOrigin.Begin);

			GZipInputStream inStream = new GZipInputStream(ms);
			byte[] buf2 = new byte[buf.Length];
			int currentIndex = 0;
			int count = buf2.Length;
			
			while (true) {
				int numRead = inStream.Read(buf2, currentIndex, count);
				if (numRead <= 0) {
					break;
				}
				currentIndex += numRead;
				count -= numRead;
			}

			Assert.AreEqual(0, count);
			
			for (int i = 0; i < buf.Length; ++i) {
				Assert.AreEqual(buf2[i], buf[i]);
			}
		}
Beispiel #4
0
 private static byte[] Decompress(byte[] compressed)
 {
     using (var compressedMemoryStream = new MemoryStream(compressed))
     {
         var gZipInputStream = new GZipInputStream(compressedMemoryStream);
         try
         {
             using (var unCompressedStream = new MemoryStream())
             {
                 var noOfBytesReadTotal = 0;
                 const int blockSize = 2048;
                 var byteBlock = new byte[blockSize];
                 while (true)
                 {
                     var noOfBytesRead = gZipInputStream.Read(byteBlock, 0, byteBlock.Length);
                     if (noOfBytesRead <= 0)
                     {
                         break;
                     }
                     noOfBytesReadTotal += noOfBytesRead;
                     unCompressedStream.Write(byteBlock, 0, noOfBytesRead);
                 }
                 var decompressedWithoutTrailingZeros =
                     unCompressedStream.GetBuffer().Take(noOfBytesReadTotal);
                 return decompressedWithoutTrailingZeros.ToArray();
             }
         }
         finally
         {
             gZipInputStream.Close();
         }
     }
 }
Beispiel #5
0
        public void TestGZip()
        {
            MemoryStream ms = new MemoryStream();
            GZipOutputStream outStream = new GZipOutputStream(ms);

            byte[] buf = new byte[100000];
            System.Random rnd = new Random();
            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Flush();
            outStream.Finish();

            ms.Seek(0, SeekOrigin.Begin);

            GZipInputStream inStream = new GZipInputStream(ms);
            byte[] buf2 = new byte[buf.Length];
            int    pos  = 0;
            while (true) {
                int numRead = inStream.Read(buf2, pos, 4096);
                if (numRead <= 0) {
                    break;
                }
                pos += numRead;
            }

            for (int i = 0; i < buf.Length; ++i) {
                Assertion.AssertEquals(buf2[i], buf[i]);
            }
        }
        public string Decompress(string filename)
        {
            StringBuilder result = null;

            try
            {
                Stream s = new GZipInputStream(File.OpenRead(filename));

                result = new StringBuilder(8192);
                UTF7Encoding encoding = new UTF7Encoding(true);

                int size = 2048;
                byte[] writeData = new byte[2048];
                while (true)
                {
                    size = s.Read(writeData, 0, size);
                    if (size > 0)
                    {
                       result.Append(encoding.GetString(writeData,0,size));
                    }
                    else
                    {
                        break;
                    }
                }
                s.Close();

            } // end try
            catch (GZipException)
            {
                throw new Exception("Error: The file being read contains invalid data.");
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Error:The file specified was not found.");
            }
            catch (ArgumentException)
            {
                throw new Exception("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
            }
            catch (PathTooLongException)
            {
                throw new Exception("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)
            {
                throw new Exception("Error: The specified path is invalid, such as being on an unmapped drive.");
            }
            catch (IOException)
            {
                throw new Exception("Error: An I/O error occurred while opening the file.");
            }
            catch (UnauthorizedAccessException)
            {
                throw new Exception("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
            }

            return result.ToString();
        }
 protected override void CreateReader()
 {
     _logFileStream = new GZipInputStream(File.Open(_logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
     if (_currentPosition > 0)
     {
         long seekPosition = 0;
         int readBlockSize = 2;//1024*1024;
         byte[] temp = new byte[readBlockSize];
         while (seekPosition < _currentPosition - readBlockSize)
         {
             _logFileStream.Read(temp, 0, readBlockSize);
             seekPosition += readBlockSize;
         }
         _logFileStream.Read(temp, 0, (int)(_currentPosition - seekPosition));
     }
     _logFileStreamReader = new StreamReader(_logFileStream, _logFileEncoding);
 }
Beispiel #8
0
    private static byte[] Unpack(byte[] _bytes)
    {
        ICSharpCode.SharpZipLib.GZip.GZipInputStream _GZipStream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(new MemoryStream(_bytes));
        byte[] _buffer2 = new byte[99999];
        int    count    = _GZipStream.Read(_buffer2, 0, _buffer2.Length);

        return(Substr(_buffer2, count));
    }
Beispiel #9
0
        public ArcFileSystem()
        {
            data = new byte[90016];

            GZipInputStream gzipStream = new GZipInputStream(new FileStream("Assets/SilentHill3/Archives/arc.arc", FileMode.Open, FileAccess.Read));

            gzipStream.Read(data, 0, 90016);

            ReadAll();
        }
Beispiel #10
0
    public static byte[] DeCompress( byte[] bytesToDeCompress )
    {
        byte[] rebyte = new byte[ bytesToDeCompress.Length * 20 ];

        MemoryStream ms = new MemoryStream( bytesToDeCompress );
        MemoryStream outStream = new MemoryStream();

        GZipInputStream s = new GZipInputStream( ms );
        int read = s.Read( rebyte , 0 , rebyte.Length );
        while ( read > 0 )
        {
            outStream.Write( rebyte, 0 , read );
            read = s.Read( rebyte , 0, rebyte.Length );
        }

        s.Close();

        return outStream.ToArray();
    }
Beispiel #11
0
        /// <summary>
        /// Decompresses specified DataSet data what is compressed with GZIP.
        /// </summary>
        /// <param name="source">Stream to decompress.</param>
        /// <returns>Returns decompressed DataSet.</returns>
        public static DataSet DecompressDataSet(Stream source)
        {
            source.Position = 0;

            GZipInputStream gzip = new GZipInputStream(source);

            MemoryStream retVal = new MemoryStream();
            byte[] buffer = new byte[8000];
            int readedCount = gzip.Read(buffer,0,buffer.Length);
            while(readedCount > 0){
                // Store current zipped data block
                retVal.Write(buffer,0,readedCount);

                // Read next data block
                readedCount = gzip.Read(buffer,0,buffer.Length);
            }

            retVal.Position = 0;
            DataSet ds = new DataSet();
            ds.ReadXml(retVal);

            return ds;

            /* mono won't support compression
            GZipStream gzip = new GZipStream(source,CompressionMode.Decompress);

            MemoryStream retVal = new MemoryStream();
            byte[] buffer = new byte[8000];
            int readedCount = gzip.Read(buffer,0,buffer.Length);
            while(readedCount > 0){
                // Store current zipped data block
                retVal.Write(buffer,0,readedCount);

                // Read next data block
                readedCount = gzip.Read(buffer,0,buffer.Length);
            }

            retVal.Position = 0;
            DataSet ds = new DataSet();
            ds.ReadXml(retVal);

            return ds;*/
        }
Beispiel #12
0
 private static string ComputeHashFromGz(string filename)
 {
     string newHash;
     using (GZipInputStream logFileStream = new GZipInputStream(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
     {
         byte[] headBytes = new byte[BytesToRead];
         logFileStream.Read(headBytes, 0, BytesToRead);
         SHA1 sha1 = SHA1.Create();
         newHash = BitConverter.ToString(sha1.ComputeHash(headBytes));
     }
     return newHash;
 }
Beispiel #13
0
 /// <summary>
 /// 解压缩
 /// </summary>
 public static byte[] Uncompression(byte[] src)
 {
     if (!IsGZip(src)) return src;
     GZipInputStream gis = new GZipInputStream(new MemoryStream(src));
     MemoryStream ms = new MemoryStream();
     int count = 0;
     byte[] tmp = new byte[4096];
     while ((count = gis.Read(tmp, 0, tmp.Length)) > 0)
     {
         ms.Write(tmp, 0, count);
     }
     gis.Close();
     return ms.ToArray();
 }
Beispiel #14
0
    public string unzip(byte[] input)
    {
        using (MemoryStream memory = new MemoryStream()) {
            using (GZipInputStream gzip = new GZipInputStream(new MemoryStream(input))) {
                int n = 0;
                byte[] temp = new byte[1024];

                while ((n = gzip.Read(temp, 0, temp.Length)) != 0)
                    memory.Write (temp, 0, n);
            }//using

            return Encoding.UTF8.GetString (memory.ToArray ());
        }//using
    }
Beispiel #15
0
 public static string returnUnZippedString(byte[] zippedByteArrayToUnzip)
 {
     MemoryStream memStream = new MemoryStream(zippedByteArrayToUnzip);
     GZipInputStream objGZipInputStream = new GZipInputStream(memStream);
     int totalNumberOfBytesRead = 0;
     StringWriter unZippedData = new StringWriter();
     while (true)
     {
         byte[] tempUncompressedData = new byte[65536];
         int numberOfBytesProcessed = objGZipInputStream.Read(tempUncompressedData,0,65536);
         if (0 == numberOfBytesProcessed) break;
         unZippedData.Write(Encoding.UTF8.GetChars(tempUncompressedData));
         totalNumberOfBytesRead += numberOfBytesProcessed;
     }
     return unZippedData.ToString();
 }
Beispiel #16
0
    public static byte[] Decompression(byte[] buf)
    {
        MemoryStream ms = new MemoryStream();
        int count = 0;

        GZipInputStream zip = new GZipInputStream(new MemoryStream(buf));
        byte[] data = new byte[256];

        while ((count = zip.Read(data, 0, data.Length)) != 0)
        {
            ms.Write(data, 0, count);
        }

        byte[] result = ms.ToArray();
        ms.Close();

        return result;
    }
        public string GUnzip(byte[] gzBuffer)
        {
            using (var ms = new MemoryStream())
            {
                var msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                var buffer = new byte[msgLength];

                ms.Position = 0;
                using (var zipStream = new GZipInputStream(ms))
                {
                    zipStream.Read(buffer, 0, buffer.Length);
                }

                return Encoding.UTF8.GetString(buffer);
            }
        }
        public static BlockAccessor decode(BlockAccessor block)
        {
            byte[] data = new byte[block.Length];
            if (block.Read(data,0,(int)block.Length) != block.Length) {
                throw new Exception("BlockAccessor partial read");
            }
            MemoryStream ms = new MemoryStream(data);
            BinaryReader reader = new BinaryReader(ms);
            UInt32 uncompressed_length = reader.ReadUInt32();
            byte[] uncompressed_data = new byte[uncompressed_length];
            GZipInputStream uncompressed_stream = new GZipInputStream(ms);

            if (uncompressed_stream.Read(uncompressed_data, 0, (int)uncompressed_length)
                != uncompressed_length) {
                throw new Exception("GZipInputStream partial read");
            }

            return new BlockAccessor(uncompressed_data);
        }
Beispiel #19
0
 public static void GzipDecompressFile(string inputFile, string outputFile)
 {
     using (var fileStreamIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
     {
         using (var fileStreamOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
         {
             using (var zipInStream = new GZipInputStream(fileStreamIn))
             {
                 int size;
                 byte[] buffer = new byte[_defaultBufferSize];
                 do
                 {
                     size = zipInStream.Read(buffer, 0, buffer.Length);
                     fileStreamOut.Write(buffer, 0, size);
                 } while (size > 0);
             }
         }
     }
 }
Beispiel #20
0
 protected override IEnumerator ReceiveCallback(WWW www, string id)
 {
     yield return www;
     if (www.error == null)
     {
         string contentEncoding = string.Empty;
         foreach (var kvp in www.responseHeaders)
         {
             if (kvp.Key.Equals(CONTENT_ENCODING, System.StringComparison.OrdinalIgnoreCase) == true)
             {
                 contentEncoding = kvp.Key;
                 break;
             }
         }
         if (string.IsNullOrEmpty(contentEncoding) == false && www.responseHeaders[contentEncoding].Equals(ENCODING_GZIP, System.StringComparison.OrdinalIgnoreCase) == true)
         {
             using (MemoryStream inputStream = new MemoryStream())
             {
                 using (GZipInputStream zipStream = new GZipInputStream(new MemoryStream(www.bytes)))
                 {
                     byte[] buffer = new byte[4096];
                     int count = 0;
                     do
                     {
                         count = zipStream.Read(buffer, 0, www.bytes.Length);
                         inputStream.Write(buffer, 0, count);
                     } while (count != 0);
                 }
                 byte[] receiveData = inputStream.ToArray();
                 OnReceive(id, receiveData, 0, receiveData.Length);
             }
         }
         else
         {
             OnReceive(id, www.bytes, 0, www.bytes.Length);
         }
     }
     else
         OnError(id, www.error, null);
     www.Dispose();
 }
Beispiel #21
0
		/// <summary>
		/// 解压字节数组
		/// </summary>
		/// <param name="inputBytes">Input bytes.</param>
		public static byte[] Decompress (byte[] inputBytes)
		{
			using (var inputStream = new MemoryStream (inputBytes))
			{
				using (var zipStream = new GZipInputStream (inputStream))
				{
					using (var outStream = new MemoryStream ())
					{
						int size = 2048;
						var outBytes = new byte[size];
						while (size > 0)
						{
							size = zipStream.Read (outBytes, 0, size);
							if (size > 0) outStream.Write (outBytes, 0, size);
						}
						zipStream.Close ();
						return outStream.ToArray ();
					}
				}
			}
		}
Beispiel #22
0
        protected override BlockingStream GetTarStream(IAsset asset)
        {
            const int      chunk          = 4096;
            BlockingStream blockingStream = new BlockingStream(1000);

            asset.GetStreamable().GetStreamAsync()
            .ContinueWith(task =>
            {
                var stream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(task.GetAwaiter().GetResult());
                Task.Factory.StartNew(() =>
                {
                    int read;
                    var buffer = new byte[chunk];
                    do
                    {
                        read = stream.Read(buffer, 0, chunk);
                        blockingStream.Write(buffer, 0, read);
                    } while (read == chunk);
                    blockingStream.Close();
                });
            });
            return(blockingStream);
        }
Beispiel #23
0
 /// <summary>
 /// 解压缩byte数组
 /// </summary>
 /// <param name="inBytes">需要解压缩的byte数组</param>
 /// <returns></returns>
 public static byte[] DecompressByte(byte[] inBytes)
 {
     byte[] writeData = new byte[2048];
     MemoryStream inStream = new MemoryStream(inBytes);
     Stream zipStream = new GZipInputStream(inStream) as Stream;
     MemoryStream outStream = new MemoryStream();
     while (true)
     {
         int size = zipStream.Read(writeData, 0, writeData.Length);
         if (size > 0)
         {
             outStream.Write(writeData, 0, size);
         }
         else
         {
             break;
         }
     }
     inStream.Close();
     byte[] outData = outStream.ToArray();
     outStream.Close();
     return outData;
 }
Beispiel #24
0
		/// <summary> Returns an gunzipped copy of the input array, truncated to
		/// <code>sizeLimit</code> bytes, if necessary.  If the gzipped input
		/// has been truncated or corrupted, a best-effort attempt is made to
		/// unzip as much as possible.  If no data can be extracted
		/// <code>null</code> is returned.
		/// </summary>
		public static byte[] GUnzipBestEffort(System.IO.Stream srcStream, int sizeLimit)
		{
			try
			{
				// decompress using GZIPInputStream 
				System.IO.MemoryStream outStream = new System.IO.MemoryStream(EXPECTED_COMPRESSION_RATIO * (Int32)srcStream.Length);
				
				GZipInputStream inStream = new GZipInputStream(srcStream);
								
				byte[] buf = new byte[BUF_SIZE];
				int size = BUF_SIZE;
				int written = 0;
				while (true) 
				{
					size = inStream.Read(buf, 0, size);
					if (size <= 0)
					{
						break;
					}
					if ((written + size) > sizeLimit)
					{
						outStream.Write(buf, 0, sizeLimit - written);
					}
					outStream.Write(buf, 0, size);
					written += size;
				}
				inStream.Close();
				
				return outStream.ToArray();
			}
			catch(Exception ex)
			{
				System.Diagnostics.Trace.WriteLine(ex.Message);
				return null;
			}
		}
 // TODO: Create a class Archive.cs and do all the archiving stuff there!
 public static bool UncompressGzipFile(string Filename, string To, Gtk.ProgressBar bar)
 {
     try{
         Console.WriteLine(Filename);
         GZipInputStream gzipIn = new GZipInputStream(File.OpenRead(Filename));
         FileStream streamWriter = File.Create(To+Path.GetFileNameWithoutExtension(Filename));
         long size=0;
         byte[] data = new byte[1024];
         while (true)
             {
             size = gzipIn.Read(data, 0, data.Length);
             if (size > 0) streamWriter.Write(data, 0, (int) size);
             else break;
         }
         streamWriter.Close();
         Console.WriteLine("Deflating the gzip file done!");
         return true;
     }
     catch(Exception e) {
         Console.WriteLine("An exception occured while deflating the gzip file: "+e.Message);
         return false;
     }
 }
Beispiel #26
0
 private byte[] DecompressBytes(byte[] rawBytes)
 {
     byte[] buffer = new byte[4096];
     using (var stream = new MemoryStream(rawBytes))
     using (var gzip = new GZipInputStream(stream))
     using (var outMs = new MemoryStream(rawBytes.Length))
     {
         int bytesRead = 0;
         while ((bytesRead = gzip.Read(buffer, 0, buffer.Length)) > 0)
         {
             outMs.Write(buffer, 0, bytesRead);
         }
         return outMs.ToArray();
     }
 }
Beispiel #27
0
        public static byte[] DecodeBase64ToBinary(string input)
        {
            // Fix for issue #429.  See comment up in EncodeBase64() method above for an explanation:
            string massagedInput = input.Replace(',','/');

            byte[] inputBuffer = Convert.FromBase64String(massagedInput);

            using (var inputStream = new MemoryStream(inputBuffer))
            {
                // mono requires an installed zlib library for GZipStream to work :(
                //using (var zipStream = new GZipStream(inputStream, CompressionMode.Decompress))
                using (var zipStream = new GZipInputStream(inputStream))
                {
                    using (var decompressedStream = new MemoryStream())
                    {
                        var buffer = new byte[4096];
                        int read;

                        while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            decompressedStream.Write(buffer, 0, read);
                        }

                        return decompressedStream.ToArray();
                    }
                }
            }
        }
Beispiel #28
0
        public void FromCompressedByteArray(byte[] compressed)
        {
            byte[] streambuff = new byte[XSize * YSize * ZSize * Voxel.BYTE_SIZE];

            using (var ms = new MemoryStream(compressed))
            {

                using (var gzs = new GZipInputStream(ms))
                {
                    gzs.Read(streambuff, 0, streambuff.Length);
                }
            }

            bool convertAlpha = true;
            for (int i = 0; i < (streambuff.Length<50?streambuff.Length:50); i++)
            {
                if (streambuff[(streambuff.Length - 1) - i] != 0) convertAlpha = false;
            }

            Voxels = new Voxel[XSize * YSize * ZSize];

            int o = 0;
            byte[] buffer = new byte[Voxel.BYTE_SIZE];
            for (int x = 0; x < XSize; x++)
                for (int y = 0; y < YSize; y++)
                    for (int z = 0; z < ZSize; z++)
                    {
                        if (!convertAlpha)
                        {
                            for (int i = 0; i < Voxel.BYTE_SIZE; i++) buffer[i] = streambuff[o + i];
                            Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer);
                            o += Voxel.BYTE_SIZE;
                        }
                        else
                        {
                            for (int i = 0; i < Voxel.BYTE_SIZE - 1; i++) buffer[i] = streambuff[o + i];
                            buffer[5] = 255;
                            Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer);
                            o += Voxel.BYTE_SIZE - 1;
                        }
                    }
        }
Beispiel #29
0
        public void TrailingGarbage()
        {
            /* ARRANGE */
            var ms = new MemoryStream();
            var outStream = new GZipOutputStream(ms);

            // input buffer to be compressed
            byte[] buf = new byte[100000];
            var rnd = new Random();
            rnd.NextBytes(buf);

            // compress input buffer
            outStream.Write(buf, 0, buf.Length);
            outStream.Flush();
            outStream.Finish();

            // generate random trailing garbage and add to the compressed stream
            byte[] garbage = new byte[4096];
            rnd.NextBytes(garbage);
            ms.Write(garbage, 0, garbage.Length);

            // rewind the concatenated stream
            ms.Seek(0, SeekOrigin.Begin);

            /* ACT */
            // decompress concatenated stream
            var inStream = new GZipInputStream(ms);
            byte[] buf2 = new byte[buf.Length];
            int currentIndex = 0;
            int count = buf2.Length;
            while (true) {
                int numRead = inStream.Read(buf2, currentIndex, count);
                if (numRead <= 0) {
                    break;
                }
                currentIndex += numRead;
                count -= numRead;
            }

            /* ASSERT */
            Assert.AreEqual(0, count);
            for (int i = 0; i < buf.Length; ++i) {
                Assert.AreEqual(buf2[i], buf[i]);
            }
        }
 public static byte[] Decompress(byte[] data)
 {
     if (data == null) return null;
     byte[] decompressedData = null;
     MemoryStream ms = null;
     GZipInputStream gzip = null;
     try
     {
         ms = new MemoryStream(data,false);
         using (BinaryReader reader = new BinaryReader(ms))
         {
             bool compressedFlag = reader.ReadBoolean();
             if (compressedFlag == false)
             {
                 //Uncompressed
                 decompressedData = reader.ReadBytes(data.Length - 1);
             }
             else
             {
                 //Decompress
                 Int32 size = reader.ReadInt32();
                 gzip = new GZipInputStream(ms);
                 decompressedData = new byte[size];
                 gzip.Read(decompressedData, 0, decompressedData.Length);
                 gzip.Close();
                 ms.Close();
             }
             reader.Close();
         }
     }
     catch
     {
         return null;
     }
     finally
     {
         if (gzip != null) gzip.Dispose();
         if (ms != null) ms.Dispose();
     }
     return decompressedData;
 }
Beispiel #31
0
        private void ExtractFirmwareArchive()
        {
            string ipod_package = null;

            foreach(DirectoryInfo directory in new DirectoryInfo(ResourcesPath).GetDirectories("iPod*.pkg")) {
                ipod_package = directory.FullName;
                break;
            }

            string cpio_out = Path.Combine(work_base, "firmware.cpio");
            string gzcpio_in = Path.Combine(ipod_package,
                "Contents" + Path.DirectorySeparatorChar + "Archive.pax.gz");

            // this sucks, it seems necessary to create an extracted CPIO archive
            // because GZipInputStream can't do seeking... CPIO needs seeking

            FileStream in_stream = new FileStream(gzcpio_in, FileMode.Open, FileAccess.Read);
            FileStream out_stream = new FileStream(cpio_out, FileMode.Create);
            GZipInputStream gzstream = new GZipInputStream(in_stream);

            byte [] block = new byte[4096];
            int size = block.Length;

            while(true) {
                size = gzstream.Read(block, 0, size);
                if(size > 0) {
                    out_stream.Write(block, 0, size);
                } else {
                    break;
                }
            }

            in_stream.Close();
            out_stream.Close();

            CpioArchive archive = new CpioArchive(cpio_out);
            foreach(CpioFileEntry entry in archive) {
                string filename = Path.GetFileName(entry.FileName);
                if(filename.StartsWith("Firmware-")) {
                    string image_path = Path.Combine(firmware_export_path, filename);
                    archive.ExtractEntry(entry, image_path);
                    images.Add(image_path);
                }
            }

            File.Delete(cpio_out);
        }
Beispiel #32
0
		/// <summary> Returns an gunzipped copy of the input array.  </summary>
		/// <throws>  IOException if the input cannot be properly decompressed </throws>
		public static byte[] GUnzip(System.IO.Stream srcStream)
		{
			// decompress using GZIPInputStream 
			System.IO.MemoryStream outStream = new System.IO.MemoryStream((Int32)(EXPECTED_COMPRESSION_RATIO * srcStream.Length));
			
			GZipInputStream inStream = new GZipInputStream(srcStream);
			
			byte[] buf = new byte[BUF_SIZE];
			int size = BUF_SIZE;
			while (true)
			{
				size = inStream.Read(buf, 0, size);
				if (size <= 0)
				{
					break;
				}
				outStream.Write(buf, 0, size);
			}
			outStream.Close();
			
			return outStream.ToArray();
		}