Beispiel #1
0
        public static void CreateHugeGzipFile()
        {
            if (System.IO.File.Exists(fileName))
            {
                return;
            }

            byte[] buffer = new byte[ONE_MEGABYTE]; // 1MB

            using (System.IO.FileStream gzipTargetAsStream = System.IO.File.OpenWrite(fileName))
            {
                using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzipStream =
                           new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(gzipTargetAsStream))
                {
                    try
                    {
                        for (int i = 0; i < REPEAT_COUNT; ++i)
                        {
                            gzipStream.Write(buffer, 0, ONE_MEGABYTE);
                        } // Next i
                    }
                    catch (System.Exception ex)
                    {
                        System.Console.WriteLine(ex.Message);
                    }
                } // End Using gzipStream
            }     // End Using gzipTargetAsStream
        }         // End Sub CreateHugeGzipFile
Beispiel #2
0
        private void CompresNowGZip()
        {
            Stream s   = null;
            Stream aux = new MemoryStream();

            try
            {
                s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(aux);

                s.Write(this.m_byteinput, 0, (int)this.m_byteinput.Length);

                (s as ICSharpCode.SharpZipLib.GZip.GZipOutputStream).Finish();

                s.Flush();
            }
            catch (ZipException e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            finally
            {
                aux.Seek(0, SeekOrigin.Begin);
                this.m_byteoutput = null;
                this.m_byteoutput = new byte[aux.Length];
                aux.Read(this.m_byteoutput, 0, (int)aux.Length);
                this.m_streamoutput = null;
                this.m_streamoutput = new MemoryStream(this.m_byteoutput);
                //s.Close();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Compresses a byte array using the specified compression, and returns a compressed byte array.
        /// </summary>
        /// <param name="bytes">The input bytes to compress.</param>
        /// <param name="offset">The amount of offset to apply to the byte array before beginning compression.</param>
        /// <param name="length">The length of bytes to compress in the byte array.</param>
        /// <param name="compressionType">Type of the compression to apply.</param>
        /// <returns>A byte array representing the compressed byte array</returns>
        public static byte[] CompressToBytes(byte[] bytes, int offset, int length, CompressionType compressionType)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(new byte[0]);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero");
            }
            if (length > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length");
            }
            if (length + offset > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length");
            }

            using (MemoryStream memoryStream = new MemoryStream()) {
                switch (compressionType)
                {
                case CompressionType.BZip:
                    using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memoryStream)) {
                        stream.Write(bytes, offset, length);
                    }
                    break;

                case CompressionType.GZip:
                    using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) {
                        stream.Write(bytes, offset, length);
                    }
                    break;

                // case CompressionType.Tar:
                //    using (ICSharpCode.SharpZipLib.Tar.TarOutputStream stream = new ICSharpCode.SharpZipLib.Tar.TarOutputStream(memoryStream)) {
                //        ICSharpCode.SharpZipLib.Tar.TarEntry entry = ICSharpCode.SharpZipLib.Tar.TarEntry.CreateTarEntry("TarEntry");
                //        entry.Size = length;
                //        stream.PutNextEntry(entry);
                //        stream.Write(bytes, offset, length);
                //        stream.IsStreamOwner = false;
                //        stream.CloseEntry();
                //    }
                //    break;
                case CompressionType.Zip:
                    using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(memoryStream)) {
                        stream.Write(bytes, offset, length);
                    }
                    break;

                default:
                    return(new byte[0]);
                }

                return(memoryStream.ToArray());
            }
        }
Beispiel #4
0
        /// <summary>
        /// 压缩byte[]
        /// </summary>
        /// <returns>The bytes.</returns>
        /// <param name="data">Data.</param>
        public static byte[] CompressBytes(byte[] data)
        {
            MemoryStream ms = new MemoryStream();

            ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzp = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms);
            gzp.Write(data, 0, data.Length);
            gzp.Close();
            return(ms.ToArray());
        }
Beispiel #5
0
 public static void WriteToGzip(this IEnumerable <byte> array, string file)
 {
     using (var gzip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(File.Create(file + ".gz")))
     {
         gzip.SetLevel(9);
         var buffer = array.ToArray();
         gzip.Write(buffer, 0, buffer.Length);
     }
 }
Beispiel #6
0
        /// <summary>
        /// 压缩字节数组
        /// 返回:已压缩的字节数组
        /// </summary>
        /// <param name="byteData">待压缩的字节数组</param>
        /// <returns></returns>
        public static byte[] CompressBytes(byte[] byteData)
        {
            var o = new MemoryStream();
            var s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(o);

            s.Write(byteData, 0, byteData.Length);
            s.Close();
            o.Flush();
            o.Close();
            return(o.ToArray());
        }
Beispiel #7
0
        private void _CompressGZ(string pathFileTAR, IDTSComponentEvents componentEvents)
        {
            using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(System.IO.File.Create(pathFileTAR)))
            {
                gzip.SetLevel(9);
                _CompressTAR(pathFileTAR, componentEvents, gzip);
                //gzip.Flush();
                //gzip.Close();
            }

            _Check_GZ(pathFileTAR, componentEvents);
        }
Beispiel #8
0
        static void Convert(string file, Dictionary <ushort, ushort> mapping)
        {
            List <KeyValuePair <Vector3Int, byte[]> > chunks = new List <KeyValuePair <Vector3Int, byte[]> >();

            using (FileStream baseStream = File.OpenRead(file))
                using (var decompressor = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(baseStream))
                    using (BinaryReader binaryReader = new BinaryReader(decompressor)) {
                        while (binaryReader.ReadBoolean())
                        {
                            Vector3Int position = new Vector3Int(
                                binaryReader.ReadInt32(),
                                binaryReader.ReadInt32(),
                                binaryReader.ReadInt32()
                                );
                            int    dataLength = binaryReader.ReadInt32();
                            byte[] data       = binaryReader.ReadBytes(dataLength);

                            chunks.Add(new KeyValuePair <Vector3Int, byte[]>(position, data));
                        }
                    }


            string fileName = Path.GetFileName(file);

            Log.Write("Loaded {0}", fileName);
            ushort[] raw = new ushort[4096];

            using (FileStream baseStream = File.Open(Path.Combine(pathRegionFolderNew, fileName), FileMode.Create, FileAccess.Write))
                using (var compressor = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(baseStream))
                    using (BinaryWriter binaryWriter = new BinaryWriter(compressor)) {
                        foreach (var pair in chunks)
                        {
                            FileSaver.FileCompression.DecompressRLEToRaw(pair.Value, raw);
                            for (int i = 0; i < 4096; i++)
                            {
                                raw[i] = mapping[raw[i]];
                            }
                            byte[] chunk = FileSaver.FileCompression.CompressRawToRLE(raw);
                            binaryWriter.Write(true);
                            binaryWriter.Write(pair.Key.x);
                            binaryWriter.Write(pair.Key.y);
                            binaryWriter.Write(pair.Key.z);
                            binaryWriter.Write(chunk.Length);
                            binaryWriter.Write(chunk);
                        }
                        binaryWriter.Write(false);
                    }

            Log.Write("Converted {0}", fileName);
        }
Beispiel #9
0
        public static byte[] GCompress(byte[] data)
        {
            byte[] result = null;

            using (MemoryStream ms = new MemoryStream())
                using (Stream s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms))
                {
                    s.Write(data, 0, data.Length);
                    s.Close();
                    result = ms.ToArray();
                }

            return(result);
        }
Beispiel #10
0
        private static void CreateZipFile(string lpSourceFile, string lpZipFile, GZipResult result)
        {
            byte[]     buffer;
            FileStream fsOut = null;
            FileStream fsIn  = null;

            ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzip = null;
            // compress the file into the zip file
            try
            {
                fsOut = new FileStream(lpZipFile, FileMode.Create, FileAccess.Write, FileShare.None);
                gzip  = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(fsOut);

                fsIn   = new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                buffer = new byte[fsIn.Length];
                fsIn.Read(buffer, 0, buffer.Length);
                fsIn.Close();
                fsIn = null;

                // compress to the zip file
                gzip.Write(buffer, 0, buffer.Length);

                result.ZipFileSize        = fsOut.Length;
                result.CompressionPercent = GetCompressionPercent(result.TempFileSize, result.ZipFileSize);
            }
            catch
            { //(Exception ex1)
                result.Errors = true;
            }
            finally
            {
                if (gzip != null)
                {
                    gzip.Close();
                    gzip = null;
                }
                if (fsOut != null)
                {
                    fsOut.Close();
                    fsOut = null;
                }
                if (fsIn != null)
                {
                    fsIn.Close();
                    fsIn = null;
                }
            }
        }
Beispiel #11
0
        public void StoreEntries(IEnumerable <string> entries)
        {
            using (var compressedStream = bucketStreamProvider.OpenWrite(address))
                using (var zip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(compressedStream))
                    using (var bw = new BinaryWriter(zip))
                    {
                        zip.SetLevel(9);
                        bw.Write((int)entries.Count());

                        foreach (var e in entries)
                        {
                            var bytes = Encoding.UTF8.GetBytes(e);
                            bw.Write((int)bytes.Length);
                            zip.Write(bytes, 0, bytes.Length);
                        }
                    }
        }
Beispiel #12
0
        public static Byte[] Compress(this byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(new Byte[0]);
            }

            using (System.IO.MemoryStream wynik = new System.IO.MemoryStream())
            {
                using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream zip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(wynik))
                {
                    zip.Write(bytes, 0, bytes.Length);
                    zip.Finish();
                    return(wynik.ToArray());
                }
            }
        }
Beispiel #13
0
        /// <summary>
        /// 压缩字符串
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string Compress(string param)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(param);

            MemoryStream ms     = new MemoryStream();
            Stream       stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms);

            try
            {
                stream.Write(data, 0, data.Length);
            }
            finally
            {
                stream.Close();
                ms.Close();
            }
            return(Convert.ToBase64String(ms.ToArray()).Trim().Replace("+", "@"));
        }
Beispiel #14
0
 public ChunkedResponse()
 {
     Headers["Transfer-Encoding"] = "chunked";
     Headers["Content-Encoding"]  = "gzip";
     ContentType = "text/html; charset=utf-8";
     Contents    = stream =>
     {
         var gzip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(stream);
         using (var streamWriter = new StreamWriter(gzip))
         {
             while (true)
             {
                 streamWriter.WriteLine("Hello");
                 gzip.Flush();
                 streamWriter.Flush();
                 Thread.Sleep(1000);
             }
         }
     };
 }
        public string Compress(string inputString)
        {
            // Create the zip stream
            System.IO.MemoryStream memstream = new System.IO.MemoryStream();
            ICSharpCode.SharpZipLib.GZip.GZipOutputStream zipstream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memstream);

            // Get the bytes of the input string
            Byte[] buffer = System.Text.Encoding.Unicode.GetBytes(inputString);

            // Write the bytes to the zipstream
            zipstream.Write(buffer, 0, buffer.Length);

            // Clean up
            zipstream.Close();
            memstream.Close();

            // Get the Base64 representation of the compressed string
            buffer = memstream.ToArray();
            string Result = Convert.ToBase64String(buffer);

            return(Result);
        }
        public string Compress(string inputString)
        {
            // Create the zip stream
            System.IO.MemoryStream memstream = new System.IO.MemoryStream();
            ICSharpCode.SharpZipLib.GZip.GZipOutputStream zipstream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memstream);

            // Get the bytes of the input string
            Byte[] buffer = System.Text.Encoding.Unicode.GetBytes(inputString);

            // Write the bytes to the zipstream
            zipstream.Write(buffer, 0, buffer.Length);

            // Clean up
            zipstream.Close();
            memstream.Close();

            // Get the Base64 representation of the compressed string
            buffer = memstream.ToArray();
            string Result = Convert.ToBase64String(buffer);

            return Result;
        }
        /// <summary>
        /// Compresses a byte array using the specified compression, and returns a compressed byte array.
        /// </summary>
        /// <param name="bytes">The input bytes to compress.</param>
        /// <param name="offset">The amount of offset to apply to the byte array before beginning compression.</param>
        /// <param name="length">The length of bytes to compress in the byte array.</param>
        /// <param name="compressionType">Type of the compression to apply.</param>
        /// <returns>A byte array representing the compressed byte array</returns>
        public static byte[] CompressToBytes(byte[] bytes, int offset, int length, CompressionType compressionType)
        {
            if (bytes == null || bytes.Length == 0)
                return new byte[0];
            if (offset < 0)
                throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero");
            if (length > bytes.Length)
                throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length");
            if (length + offset > bytes.Length)
                throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length");

            using (MemoryStream memoryStream = new MemoryStream()) {
                switch (compressionType) {
                    case CompressionType.BZip:
                        using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memoryStream)) {
                            stream.Write(bytes, offset, length);
                        }
                        break;
                    case CompressionType.GZip:
                        using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) {
                            stream.Write(bytes, offset, length);
                        }
                        break;

                    // case CompressionType.Tar:
                    //    using (ICSharpCode.SharpZipLib.Tar.TarOutputStream stream = new ICSharpCode.SharpZipLib.Tar.TarOutputStream(memoryStream)) {
                    //        ICSharpCode.SharpZipLib.Tar.TarEntry entry = ICSharpCode.SharpZipLib.Tar.TarEntry.CreateTarEntry("TarEntry");
                    //        entry.Size = length;
                    //        stream.PutNextEntry(entry);
                    //        stream.Write(bytes, offset, length);
                    //        stream.IsStreamOwner = false;
                    //        stream.CloseEntry();
                    //    }
                    //    break;
                    case CompressionType.Zip:
                        using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(memoryStream)) {
                            stream.Write(bytes, offset, length);
                        }
                        break;
                    default:
                        return new byte[0];
                }

                return memoryStream.ToArray();
            }
        }
Beispiel #18
0
        private int RecursivelyPersistLayer(XbimSqliteDB db, XbimMeshLayer<XbimMeshGeometry3D, XbimRenderMaterial> layer, int layerid, int parentLayerId)
        {
            try
            {
                // bytes contain 6 floats and 1 int
                // pointX, pointY, pointZ, normalX, normalY, normalZ, entityLabel
                // this is the vbo.

                XbimTexture texture = layer.Style;
                XbimColour c = layer.Style.ColourMap[0]; //take the first one, not perhaps correct or best practice
                byte[] colour = new byte[4 * sizeof(float)];
                MemoryStream ms = new MemoryStream(colour);
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write(c.Red);
                bw.Write(c.Green);
                bw.Write(c.Blue);
                bw.Write(c.Alpha);

                byte[] vbo = ((XbimMeshGeometry3D)layer.Hidden).ToByteArray();

                MemoryStream memoryStream = new MemoryStream();
                using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream zis = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream))
                {
                    zis.Write(vbo, 0, vbo.Length);
                    memoryStream.Flush();
                }
                vbo = memoryStream.ToArray();


                // bounding box
                XbimRect3D bb = layer.BoundingBoxHidden();
                byte[] bbArray = bb.ToFloatArray();

                db.AddLayer(layer.Name, layerid, parentLayerId, colour, vbo, bbArray);

                int myParent = layerid;
                foreach (var subLayer in layer.SubLayers)
                {
                    layerid++;
                    layerid = RecursivelyPersistLayer(db, subLayer, layerid, myParent);
                }
                return layerid;

            }
            catch (Exception ex)
            {
                throw new XbimException("Error building scene layer", ex);
            }
        }
Beispiel #19
0
 private void CompresNowGZip()
 {
     Stream s = null;
     Stream aux = new MemoryStream();
     try
     {
         s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(aux);
         s.Write(this.m_byteinput, 0, (int)this.m_byteinput.Length);
         (s as ICSharpCode.SharpZipLib.GZip.GZipOutputStream).Finish();
         s.Flush();
     }
     catch (ZipException e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     finally
     {
         aux.Seek(0, SeekOrigin.Begin);
         this.m_byteoutput = null;
         this.m_byteoutput = new byte[aux.Length];
         aux.Read(this.m_byteoutput, 0, (int)aux.Length);
         this.m_streamoutput = null;
         this.m_streamoutput = new MemoryStream(this.m_byteoutput);
         //s.Close();
     }
 }