Ejemplo n.º 1
0
Archivo: Utils.cs Proyecto: biuken/eAnt
        private void CompresNowZip()
        {
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
                new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                    ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
            Stream aux = new MemoryStream();

            byte[] data = new byte[this.m_streaminput.Length];           //minimo el tamaño sin comprimir.
            int    size = 0;

            try
            {
                def.SetInput(this.m_byteinput);
                def.Finish();
                size = def.Deflate(data);
                if (size == 0)
                {
                    while (def.IsFinished == false)
                    {
                        if (def.IsNeedingInput)
                        {
                            Exception e = new Exception("Tamaño muy pequeño para comprimir");
                            break;
                        }
                        else
                        {
                            size = def.Deflate(data);
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                def.Flush();
            }
            catch (ZipException e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            finally
            {
                this.m_byteoutput   = null;
                this.m_byteoutput   = new byte[size];
                this.m_streamoutput = new MemoryStream(size);
                this.m_streamoutput.Write(data, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
                this.m_streamoutput.Read(this.m_byteoutput, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
            }
        }
Ejemplo n.º 2
0
        public static string ToDeflatedCompressedBase64String(string inp, string ns)
        {
            StringBuilder sb         = new StringBuilder();
            TextWriter    textWriter = (TextWriter) new StringWriter(sb);

            new XmlSerializer(typeof(string), ns).Serialize(textWriter, (object)inp);
            textWriter.Close();
            sb.ToString();
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater deflater = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1, true);
            MemoryStream memoryStream = new MemoryStream();

            byte[] numArray = new byte[256];
            deflater.SetInput(new UnicodeEncoding().GetBytes(inp));
            deflater.Flush();
            int count;

            do
            {
                count = deflater.Deflate(numArray, 0, numArray.Length);
                memoryStream.Write(numArray, 0, count);
            }while (count > 0);
            return(Convert.ToBase64String(memoryStream.ToArray()));
        }
Ejemplo n.º 3
0
 private void CompresNowZip()
 {
     ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
             new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
             ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
     Stream aux = new MemoryStream();
     byte[] data = new byte[this.m_streaminput.Length]; //minimo el tamaño sin comprimir.
     int size = 0;
     try
     {
         def.SetInput(this.m_byteinput);
         def.Finish();
         size = def.Deflate(data);
         if (size == 0)
             while (def.IsFinished == false)
             {
                 if (def.IsNeedingInput)
                 {
                     Exception e = new Exception("Tamaño muy pequeño para comprimir");
                     break;
                 }
                 else
                 {
                     size = def.Deflate(data);
                     System.Threading.Thread.Sleep(2000);
                 }
             }
         def.Flush();
     }
     catch (ZipException e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     catch (Exception e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     finally
     {
         this.m_byteoutput = null;
         this.m_byteoutput = new byte[size];
         this.m_streamoutput = new MemoryStream(size);
         this.m_streamoutput.Write(data, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
         this.m_streamoutput.Read(this.m_byteoutput, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
     }
 }