Read() public method

Read and decompress data from the source stream.
With a GZipStream, decompression is done through reading.
public Read ( byte buffer, int offset, int count ) : int
buffer byte The buffer into which the decompressed data should be placed.
offset int the offset within that data array to put the first byte read.
count int the number of bytes to read.
return int
Beispiel #1
0
 public static byte[] Decompress(byte[] data)
 {
     try
     {
         MemoryStream ms       = new MemoryStream(data);
         GZipStream   zip      = new GZipStream(ms, CompressionMode.Decompress, true);
         MemoryStream msreader = new MemoryStream();
         byte[]       buffer   = new byte[0x1000];
         while (true)
         {
             int reader = zip.Read(buffer, 0, buffer.Length);
             if (reader <= 0)
             {
                 break;
             }
             msreader.Write(buffer, 0, reader);
         }
         zip.Close();
         ms.Close();
         msreader.Position = 0;
         buffer            = msreader.ToArray();
         msreader.Close();
         return(buffer);
     }
     catch (IOException e)
     {
         throw new IOException(e.Message);
     }
 }
        /// <summary>
        /// Decompresses the file at the given path. Returns the path of the
        /// decompressed file.
        /// </summary>
        /// <param name="path">The path to decompress.</param>
        /// <returns>The path of the decompressed file.</returns>
        public string Decompress(string path)
        {
            string outputPath = Regex.Replace(path, @"\.gz$", String.Empty, RegexOptions.IgnoreCase);

            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }

            using (FileStream fs = File.OpenRead(path))
            {
                using (FileStream output = File.Create(outputPath))
                {
                    using (GZipStream gzip = new GZipStream(fs, CompressionMode.Decompress))
                    {
                        byte[] buffer = new byte[4096];
                        int count = 0;

                        while (0 < (count = gzip.Read(buffer, 0, buffer.Length)))
                        {
                            output.Write(buffer, 0, count);
                        }
                    }
                }
            }

            return outputPath;
        }
Beispiel #3
0
//	private void GameLoop()
//	{
//		processOnDemandQueue();
//	}

    public static byte[] GetModel(int modelId)
    {
        return(ReadAllBytes(sign.signlink.findcachedir() + "7/" + modelId + ".mdl"));

        byte[] modelData = UnityClient.decompressors [1].decompress(modelId);
        if (modelData != null)
        {
            byte[] gzipInputBuffer = new byte[modelData.Length * 100];
            int    i2 = 0;
            try {
                Ionic.Zlib.GZipStream gzipinputstream = new Ionic.Zlib.GZipStream(
                    new MemoryStream(modelData), Ionic.Zlib.CompressionMode.Decompress);
                do
                {
                    if (i2 == gzipInputBuffer.Length)
                    {
                        throw new Exception("buffer overflow!");
                    }
                    int k = gzipinputstream.Read(gzipInputBuffer, i2,
                                                 gzipInputBuffer.Length - i2);
                    if (k == 0)
                    {
                        break;
                    }
                    i2 += k;
                } while (true);
            } catch (IOException _ex) {
                throw new Exception("error unzipping");
            }
            modelData = new byte[i2];
            System.Array.Copy(gzipInputBuffer, 0, modelData, 0, i2);
        }
        return(modelData);
    }
Beispiel #4
0
 public static void ZipStreamDecompress(Stream source, Stream dest)
 {
     using (var stream = new Ionic.Zlib.GZipStream(source, Ionic.Zlib.CompressionMode.Decompress, true))
     {
         var buf = new byte[ZIP_BUFFER_SIZE];
         int len;
         while ((len = stream.Read(buf, 0, buf.Length)) > 0)
         {
             dest.Write(buf, 0, len);
         }
     }
 }
Beispiel #5
0
        public static void Decompress(byte[] data, string path)
        {
            byte[] buffer = new byte[BufferSize];
            int read;

            using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write))
            using (GZipStream gzip = new GZipStream(new MemoryStream(data), CompressionMode.Decompress, CompressionLevel.BestCompression, false))
            {
                while ((read = gzip.Read(buffer, 0, BufferSize)) > 0)
                {
                    output.Write(buffer, 0, read);
                    output.Flush();
                }
            }
        }
Beispiel #6
0
        public static byte[] Decompress(byte[] data)
        {
            MemoryStream output = new MemoryStream();
            byte[] buffer = new byte[BufferSize];
            int read;

            using (GZipStream gzip = new GZipStream(new MemoryStream(data), CompressionMode.Decompress, CompressionLevel.BestCompression, false))
            {

                while ((read = gzip.Read(buffer, 0, BufferSize)) > 0)
                {
                    output.Write(buffer, 0, read);
                }
            }

            return output.ToArray();
        }
        static public byte[] uncompress(byte[] bytes)
        {

            byte[] working = new byte[1024 * 20];
            var input = new MemoryStream(bytes);
            var output = new MemoryStream();
            using (Stream decompressor = new GZipStream(input, CompressionMode.Decompress, true))
            {

                int n;
                while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                {
                    output.Write(working, 0, n);
                }

            }
            return output.ToArray();

        }
Beispiel #8
0
 byte[] UnZip(MemoryStream output)
 {
     var cms = new MemoryStream ();
     output.Seek(0, SeekOrigin.Begin);
     using (var gz = new GZipStream(output, CompressionMode.Decompress)) {
         var buf = new byte[1024];
         int byteCount = 0;
         while ((byteCount = gz.Read(buf, 0, buf.Length)) > 0) {
             cms.Write(buf, 0, byteCount);
         }
     }
     return cms.ToArray();
 }
Beispiel #9
0
        public MultiplayerConfig LoadMultiplayer(byte[] data)
        {
            MemoryStream memoryStream = new MemoryStream(data);
            BinaryReader br = new BinaryReader(memoryStream);

            //Read compressed bytes
            int uncompressedSize = br.ReadInt32();

            //Uncompress bytes
            byte[] uncompressedBytes = new byte[uncompressedSize];
            GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
            int bytesRead = 0;

            while (true)
            {
                int read = gzipStream.Read(uncompressedBytes, bytesRead, uncompressedSize - bytesRead);

                if (read == 0)
                    break;

                bytesRead += read;
            }

            br = new BinaryReader(new MemoryStream(uncompressedBytes));

            //Load world from uncompressed bytes
            MultiplayerConfig config = LoadMultiplayer(br);

            tileManager.Create(config.tileDefinitions, sizeXbits, sizeYbits, sizeZbits);
            itemManager.Create(config.itemDefinitions);
            avatarManager.Create(config.avatarDefinitions);
            sectorManager.Create();
            dayCycleManager.Create(null);

            tileManager.Load(br);
            itemManager.Load(br);
            sectorManager.Load(br);
            dayCycleManager.Load(br);

            //Player avatar
            int playerObjectId = br.ReadInt32();
            Player player = (Player) avatarManager.CreateAvatar(avatarManager.GetAvatarDefinitionById("player"), playerObjectId, new Vector3(), false);
            player.Load(br);

            cwListener.CreateObject(player);

            gameplay.WorldLoaded();

            return config;
        }
Beispiel #10
0
 /// <summary>
 /// Decompresses a byte array using the specified compression type.
 /// </summary>
 /// <param name="bytes">The byte array to be decompressed.</param>
 /// <param name="type">Type of compression to use.</param>
 /// <returns>Decompressed byte array.</returns>
 public static byte[] Decompress(this byte[] bytes, CompressionType type)
 {
     int size = 4096;
     byte[] buffer = new byte[size];
     using (MemoryStream memory = new MemoryStream())
     {
         using (MemoryStream memory2 = new MemoryStream(bytes))
             switch (type)
             {
                 case CompressionType.Zlib:
                     using (ZlibStream stream = new ZlibStream(memory2, CompressionMode.Decompress))
                     {
                         int count = 0;
                         while ((count = stream.Read(buffer, 0, size)) > 0)
                             memory.Write(buffer, 0, count);
                     }
                     break;
                 case CompressionType.GZip:
                     using (GZipStream stream = new GZipStream(memory2, CompressionMode.Decompress))
                     {
                         int count = 0;
                         while ((count = stream.Read(buffer, 0, size)) > 0)
                             memory.Write(buffer, 0, count);
                     }
                     break;
                 default:
                     throw new ArgumentException("Unknown compression type.");
             }
         return memory.ToArray();
     }
 }
Beispiel #11
0
    private static string getUnzippedPath(string gzipPath_)
    {
      var csvPath = gzipPath_.Replace(".gz", string.Empty);

      if (File.Exists(csvPath)) return csvPath;

      var buffer = new byte[2048];
      int n = 1;

      try
      {
        if (!File.Exists(csvPath))
        {
          using (var input = File.OpenRead(gzipPath_))
          {
            using (var decompressor = new GZipStream(input, CompressionMode.Decompress, true))
            {
              using (var output = File.Create(csvPath))
              {
                do
                {
                  n = decompressor.Read(buffer, 0, buffer.Length);
                  if (n > 0) output.Write(buffer, 0, n);
                }
                while (n > 0);
              }
            }
          }
        }
      }
      catch (Exception ex_)
      {
        Logger.Error(string.Format("Error processing file {0}. error: {1}{2}", gzipPath_, ex_.Message, ex_.StackTrace), typeof(FileHelper));
      }

      return csvPath;
    }
Beispiel #12
0
        private IEnumerator Post(WWW www, Action<string> wwwSuccessCallback, Action<string> wwwErrorCallback)
        {
            yield return www;
            if (!string.IsNullOrEmpty(www.error))
            {
                wwwErrorCallback(www.error);
            }
            else
            {
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                if (PlayFabSettings.CompressApiData)
                {
                    try
                    {
                        var stream = new MemoryStream(www.bytes);
                        using (var gZipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                        {
                            var buffer = new byte[4096];
                            using (var output = new MemoryStream())
                            {
                                int read;
                                while ((read = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, read);
                                }
                                output.Seek(0, SeekOrigin.Begin);
                                var streamReader = new System.IO.StreamReader(output);
                                var jsonResponse = streamReader.ReadToEnd();
                                //Debug.Log(jsonResponse);
                                wwwSuccessCallback(jsonResponse);
                            }
                        }
                    }
                    catch
                    {
                        //if this was not a valid GZip response, then send the message back as text to the call back.
                        wwwSuccessCallback(www.text);
                    }
                }
                else
                {
#endif
                    wwwSuccessCallback(www.text);
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                }
#endif
            }
        }
Beispiel #13
0
        public void Load(Config config, byte[] data)
        {
			MemoryStream memoryStream = new MemoryStream(data);
			
            BinaryReader br = new BinaryReader(memoryStream);
			
            //Read version info
            string version = br.ReadString();
            if (version != VERSION_INFO)
                return;
			
			//Read compressed bytes
			int uncompressedSize = br.ReadInt32();
			
			//Uncompress bytes
			byte[] uncompressedBytes = new byte[uncompressedSize];
			GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
			int bytesRead = 0;
			
			while(true)
			{
				int read = gzipStream.Read(uncompressedBytes, bytesRead, uncompressedSize - bytesRead);
				
				if (read == 0)
					break;
				
				bytesRead += read;
			}
			
			br = new BinaryReader(new MemoryStream(uncompressedBytes));

			//Load world from uncompressed bytes
            Load(br, false);
			
            tileManager.Create(config.tileDefinitions, sizeXbits, sizeYbits, sizeZbits);
            itemManager.Create(config.itemDefinitions);
            avatarManager.Create(config.avatarDefinitions);
            sectorManager.Create();
            dayCycleManager.Create(config.dayInfo);

            tileManager.Load(br);
            itemManager.Load(br);
            avatarManager.Load(br);
            sectorManager.Load(br);
            dayCycleManager.Load(br);

            gameplay.WorldLoaded();
        }
    public static void UnpackDirectory(string sourcePath_, string unpackToThisDirectory_, string processedDirectory_, string errorDirectory_, string noMetaDirectory_,  bool recurse_, bool processFile_=true)
    {
      if (!Directory.Exists(sourcePath_))
        return;

      foreach(var zipPath in Directory.GetFiles(sourcePath_,"*.gz"))
      {
        var fileName = new FileInfo(zipPath).Name;
        var csvFileName = fileName.Replace(".gz", string.Empty);

        var unpackPath = string.Format(@"{0}\{1}", unpackToThisDirectory_, csvFileName);

        var buffer = new byte[2048];
        int n=1;
        Logger.Debug(string.Format("Unpacking {0}", new FileInfo(fileName).Name), typeof(FuturesIntradaySaver));

        try
        {
          if (!File.Exists(unpackPath))
          {
            using (var input = File.OpenRead(zipPath))
            {
              using (var decompressor = new GZipStream(input, CompressionMode.Decompress, true))
              {
                using (var output = File.Create(unpackPath))
                {
                  do
                  {
                    n = decompressor.Read(buffer, 0, buffer.Length);
                    if (n > 0) output.Write(buffer, 0, n);
                  }
                  while (n > 0);
                }
              }
            }
          }

          if(processFile_)
            processFile(unpackPath, processedDirectory_, errorDirectory_, noMetaDirectory_);
        }
        catch (Exception ex_)
        {
          Logger.Error( string.Format("Error processing file {0}. error: {1}{2}", fileName, ex_.Message, ex_.StackTrace),typeof(FuturesIntradaySaver));

          string errorPath = string.Format(@"{0}\{1}", errorDirectory_, csvFileName);

          if (processFile_ && File.Exists(unpackPath)) File.Move(unpackPath, errorPath);
        }
      }

      if(recurse_)
        foreach (var dir in Directory.GetDirectories(sourcePath_))
          UnpackDirectory(dir, unpackToThisDirectory_, processedDirectory_, errorDirectory_, noMetaDirectory_, recurse_,
            processFile_);
    }
Beispiel #15
0
        private IEnumerator Post(WWW www, Action<string> wwwSuccessCallback, Action<string> wwwErrorCallback)
        {
            yield return www;
            if (!string.IsNullOrEmpty(www.error))
            {
                wwwErrorCallback(www.error);
            }
            else
            {
                try
                {
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                    string encoding;
                    if (www.responseHeaders.TryGetValue("Content-Encoding", out encoding) && encoding.ToLower() == "gzip")
                    {
                        var stream = new MemoryStream(www.bytes);
                        using (var gZipStream = new GZipStream(stream, CompressionMode.Decompress, false))
                        {
                            var buffer = new byte[4096];
                            using (var output = new MemoryStream())
                            {
                                int read;
                                while ((read = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, read);
                                }
                                output.Seek(0, SeekOrigin.Begin);
                                var streamReader = new System.IO.StreamReader(output);
                                var jsonResponse = streamReader.ReadToEnd();
                                //Debug.Log(jsonResponse);
                                wwwSuccessCallback(jsonResponse);
                            }
                        }
                    }
                    else
#endif
                    {
                        wwwSuccessCallback(www.text);
                    }
                }
                catch (Exception e)
                {
                    wwwErrorCallback("Unhandled error in PlayFabWWW: " + e);
                }
            }
        }
Beispiel #16
0
    static void GenUnityMeshs()
    {
        Debug.Log("Generating Unity Mesh's");
        MapManager.InitializeSolo();
        RS2Sharp.Texture.anIntArray1468 = new int[512];
        RS2Sharp.Texture.anIntArray1469 = new int[2048];
        RS2Sharp.Texture.SINE           = new int[2048];
        RS2Sharp.Texture.COSINE         = new int[2048];
        for (int i = 1; i < 512; i++)
        {
            RS2Sharp.Texture.anIntArray1468[i] = 32768 / i;
        }

        for (int j = 1; j < 2048; j++)
        {
            RS2Sharp.Texture.anIntArray1469[j] = 0x10000 / j;
        }

        for (int k = 0; k < 2048; k++)
        {
            RS2Sharp.Texture.SINE[k]   = (int)(65536D * System.Math.Sin((double)k * 0.0030679614999999999D));
            RS2Sharp.Texture.COSINE[k] = (int)(65536D * System.Math.Cos((double)k * 0.0030679614999999999D));
        }
        RS2Sharp.Texture.method372(0.80000000000000004D);
        byte[] gzipInputBuffer = new byte[0];
        for (int modelId = 0; modelId < 1000; ++modelId)
        {
            byte[] modelData = UnityClient.decompressors[1].decompress(modelId);
            if (modelData != null)
            {
                gzipInputBuffer = new byte[modelData.Length * 100];
                int i2 = 0;
                try {
                    Ionic.Zlib.GZipStream gzipinputstream = new Ionic.Zlib.GZipStream(
                        new MemoryStream(modelData), Ionic.Zlib.CompressionMode.Decompress);
                    do
                    {
                        if (i2 == gzipInputBuffer.Length)
                        {
                            throw new Exception("buffer overflow!");
                        }
                        int k = gzipinputstream.Read(gzipInputBuffer, i2,
                                                     gzipInputBuffer.Length - i2);
                        if (k == 0)
                        {
                            break;
                        }
                        i2 += k;
                    } while (true);
                } catch (IOException _ex) {
                    throw new Exception("error unzipping");
                }
                modelData = new byte[i2];
                System.Array.Copy(gzipInputBuffer, 0, modelData, 0, i2);

                Model model = new Model(modelData, modelId);

                RuneMesh rMesh = new RuneMesh();
                rMesh.Fill(model, true);

                Mesh mesh = new Mesh();
                rMesh.Render(mesh, false);

                if (mesh.vertexCount > 0)
                {
                    AssetDatabase.CreateAsset(mesh, "Assets/Resources/Meshes/" + modelId + ".asset");
                }
            }
            AssetDatabase.SaveAssets();
        }
    }
Beispiel #17
0
        /// <summary>
        /// Gunzips the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string Gunzip(this byte[] input) {
            var bytes = new List<byte>();
            using (var gzStr = new GZipStream(new MemoryStream(input), CompressionMode.Decompress)) {
                var bytesRead = new byte[512];
                while (true) {
                    var numRead = gzStr.Read(bytesRead, 0, 512);
                    if (numRead > 0) {
                        bytes.AddRange(bytesRead.Take(numRead));
                    }
                    else {
                        break;
                    }
                }
            }

            return bytes.ToArray().ToUtf8String();
        }
Beispiel #18
0
        protected void Unzip()
        {
            using (MemoryStream i = new MemoryStream(bytes)) {
            // Verify gzip header.
            byte[] header = new byte[10];
            if (i.Read(header, 0, header.Length) == 10 && header[0] == 0x1F && header[1] == 0x8B && header[2] == 8) {
              // Back to start.
              i.Seek(0, SeekOrigin.Begin);

              using (MemoryStream o = new MemoryStream()) {
            using (GZipStream gzip = new GZipStream(i, CompressionMode.Decompress)) {
              var buffer = new byte[1024];

              do {
                int read = gzip.Read(buffer, 0, buffer.Length);

                if (read == 0) {
                  break;
                }

                o.Write(buffer, 0, read);
              } while (true);
            }

            this.bytes = o.ToArray();
              }
            }
              }
        }