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);
     }
 }
        static public byte[] compress(byte[] bytes)
        {

            var output = new MemoryStream();
            var gzipStream = new GZipStream(output, CompressionMode.Compress, true);
            gzipStream.Write(bytes, 0, bytes.Length);
            gzipStream.Close();
            return output.ToArray();

        }
Beispiel #3
0
        public void compress(Stream inStream, Stream outStream)
        {

            GZipStream compressionStream = new GZipStream(outStream, CompressionMode.Compress, true);

            inStream.CopyTo(compressionStream);

            compressionStream.Close();

        }
Beispiel #4
0
 /// <summary>
 /// 对byte数组进行压缩
 /// </summary>
 /// <param name="data">待压缩的byte数组</param>
 /// <returns>压缩后的byte数组</returns>
 public static byte[] Compress(byte[] data)
 {
     try
     {
         MemoryStream ms  = new MemoryStream();
         GZipStream   zip = new GZipStream(ms, CompressionMode.Compress, true);
         zip.Write(data, 0, data.Length);
         zip.Close();
         byte[] buffer = new byte[ms.Length];
         ms.Position = 0;
         ms.Read(buffer, 0, buffer.Length);
         ms.Close();
         return(buffer);
     }
     catch (IOException e)
     {
         throw new IOException(e.Message);
     }
 }
Beispiel #5
0
        public void Write()
        {
            TAG_Compound data;

            using (FileStream level = new FileStream(LevelDatPath, FileMode.Open))
            {
                using (GZipStream decompress = new GZipStream(level, CompressionMode.Decompress))
                {
                    MemoryStream mem = new MemoryStream();
                    decompress.CopyTo(mem);
                    mem.Seek(0, SeekOrigin.Begin);
                    data = new TAG_Compound(mem);
                }
            }

            ((TAG_Long)data["Data"]["RandomSeed"]).Payload = Seed;

            using (FileStream level = new FileStream(LevelDatPath, FileMode.Truncate))
            {
                    MemoryStream mem = new MemoryStream();
                    GZipStream compress = new GZipStream(mem, CompressionMode.Compress);
                    data.Write(compress);
                    compress.Close();
                    byte[] buffer = mem.ToArray();
                    level.Write(buffer, 0, buffer.Length);
            }
        }
Beispiel #6
0
        public void Zlib_DisposedException_GZipStream()
        {
            string TextToCompress =  IhaveaDream;

            MemoryStream ms1= new MemoryStream();

            Stream compressor= new GZipStream(ms1, CompressionMode.Compress, false);

            TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                  TextToCompress.Length, TextToCompress);
            TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

            StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
            sw.Write(TextToCompress);
            sw.Close(); // implicitly closes compressor
            sw.Close(); // implicitly closes compressor, again

            compressor.Close(); // explicitly closes compressor
            var a = ms1.ToArray();
            TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

            var ms2 = new MemoryStream(a);
            Stream decompressor  = new GZipStream(ms2, CompressionMode.Decompress, false);

            TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

            var sr = new StreamReader(decompressor, Encoding.ASCII);
            string DecompressedText = sr.ReadToEnd();
            sr.Close();

            TestContext.WriteLine("decompressor.CanRead = {0}",decompressor.CanRead);

            TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
            TestContext.WriteLine("\n");
            Assert.AreEqual<String>(TextToCompress, DecompressedText);
        }
Beispiel #7
0
        public byte[] SaveMultiplayer(Player player)
        {
            //Save world to memory
            MemoryStream bytesStream = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(bytesStream);

            //Basic world info
            SaveMultiplayer(bw);

            //Managers
            tileManager.Save(bw);
            itemManager.Save(bw);
            sectorManager.Save(bw);
            dayCycleManager.Save(bw);

            //Player avatar
            bw.Write(player.objectId);
            player.Save(bw);

            bw.Flush();

            byte[] bytes = bytesStream.ToArray();

            //Write header
            MemoryStream bytesStreamFinal = new MemoryStream();
            BinaryWriter bwFinal = new BinaryWriter(bytesStreamFinal);

            bwFinal.Write(bytes.Length);
            bwFinal.Flush();

            //Compress and write world
            GZipStream gzipStream = new GZipStream(bytesStreamFinal, CompressionMode.Compress, CompressionLevel.BestSpeed, true);
            gzipStream.Write(bytes, 0, bytes.Length);
            gzipStream.Close();

            return bytesStreamFinal.ToArray();
        }
Beispiel #8
0
        public byte[] Save()
        {
			//Save world to memory
			MemoryStream bytesStream = new MemoryStream();
			BinaryWriter bw = new BinaryWriter(bytesStream);
			
            Save(bw, false);
            tileManager.Save(bw);
            itemManager.Save(bw);
            avatarManager.Save(bw);
            sectorManager.Save(bw);
            dayCycleManager.Save(bw);

            bw.Flush();
			
			byte[] bytes = bytesStream.ToArray();
			
			//Write header
            MemoryStream bytesStreamFinal = new MemoryStream();
            BinaryWriter bwFinal = new BinaryWriter(bytesStreamFinal);

            bwFinal.Write(VERSION_INFO);
			bwFinal.Write(bytes.Length);
			bwFinal.Flush();
			
			//Compress and write world
			GZipStream gzipStream = new GZipStream(bytesStreamFinal, CompressionMode.Compress, CompressionLevel.BestSpeed, true);
			gzipStream.Write(bytes, 0, bytes.Length);
			gzipStream.Close();

            return bytesStreamFinal.ToArray();
        }
Beispiel #9
0
        public static int Main(string[] args)
        {
            var inputFile = args[0];
            var targetFolder = args[1];

            if (!Directory.Exists(targetFolder))
                Directory.CreateDirectory(targetFolder);

            var language = _GetLanguage(args);
            var type = _GetGramType(args);
            var fictionOnly = args.Any(x => x.ToLower() == "fiction");
            var versionDate = args.Single(x => x.All(Char.IsNumber));
            var includeDependencies = args.Any(x => x.ToLower() == "include0");

            var rawHrefs = File.ReadAllLines(inputFile).Select(_GetHref).Where(x => x != null).Select(x => x.ToLower());
            var filtered = rawHrefs
                .Where(x => x.Contains(_GetNGramTypeForFilter(type).ToLower()))
                .Where(x => _FilterByLanguage(x, language))
                .Where(x => x.Contains(versionDate)).ToArray();

            var connectionString = @"Data Source=.\mssql12;Initial Catalog=NGram;Integrated Security=True";

            var oneGramLoader = new OneGramLoader();

            foreach (var rawHref in filtered)
            {
                Console.WriteLine("Downloading href {0}", rawHref);
                var req = WebRequest.CreateHttp(rawHref);
                var res = req.GetResponse();
                using (var resStream = res.GetResponseStream())
                {
                    using (var zipStream = new GZipStream(resStream, CompressionMode.Decompress))
                    {
                        using (var sr = new StreamReader(zipStream))
                        {
                            oneGramLoader.ProcessOneGram(sr, connectionString);
                        }

                        zipStream.Close();
                    }
                    resStream.Close();
                }
            }

            Console.WriteLine("Finished - any key");
            Console.ReadLine();

            return 0;
        }