Exemplo n.º 1
0
        public double?TestCompressionRatio(string file, out int testedByteCount, out int outputByteCount)
        {
            byte[] data;

            try
            {
                using (var test = File.OpenRead(file))
                {
                    if (test.Length > _maxTestLength)
                    {
                        //grab the max length from some random spot in the file
                        data          = new byte[_maxTestLength];
                        test.Position = _localRandom.NextLong(0, test.Length - _maxTestLength);;
                        test.Read(data, 0, _maxTestLength);
                    }
                    else
                    {
                        //grab full contents
                        data = new byte[test.Length];
                        test.Read(data, 0, (int)test.Length);
                    }
                }

                testedByteCount = data.Length;

                using (var rawArchiveStream = new MemoryStream())
                {
                    var m_writer = new ManagedLzma.LZMA.Master.SevenZip.ArchiveWriter(rawArchiveStream);
                    m_writer.ConnectEncoder(_lzma2Encoder);

                    using (var encoderStream = _lzma2Encoder.BeginWriteFile(new ArchiveWriterEntry {
                        Name = file
                    }))
                    {
                        encoderStream.Write(data, 0, data.Length);
                    }
                    m_writer.WriteFinalHeader();
                    m_writer.DisconnectEncoder();
                    outputByteCount = (int)m_writer.WrittenSize; //this should never get to "long" dimensions...
                }

                return((testedByteCount - outputByteCount) * 1.0 / testedByteCount);
            }
            catch (UnauthorizedAccessException)
            {
                //exact error doesn't really matter - just important to know there was an issue
                testedByteCount = 0;
                outputByteCount = 0;
                return(null);
            }
            catch (IOException)
            {
                //exact error doesn't really matter - just important to know there was an issue
                testedByteCount = 0;
                outputByteCount = 0;
                return(null);
            }
        }
        public double? TestCompressionRatio(string file, out int testedByteCount, out int outputByteCount)
        {
            byte[] data;

            try
            {
                using (var test = File.OpenRead(file))
                {
                    if (test.Length > _maxTestLength)
                    {
                        //grab the max length from some random spot in the file
                        data = new byte[_maxTestLength];
                        test.Position = _localRandom.NextLong(0, test.Length - _maxTestLength); ;
                        test.Read(data, 0, _maxTestLength);
                    }
                    else
                    {
                        //grab full contents
                        data = new byte[test.Length];
                        test.Read(data, 0, (int)test.Length);
                    }
                }

                testedByteCount = data.Length;

                using (var rawArchiveStream = new MemoryStream())
                {
                    var m_writer = new ManagedLzma.LZMA.Master.SevenZip.ArchiveWriter(rawArchiveStream);
                    m_writer.ConnectEncoder(_lzma2Encoder);

                    using (var encoderStream = _lzma2Encoder.BeginWriteFile(new ArchiveWriterEntry { Name = file }))
                    {
                        encoderStream.Write(data, 0, data.Length);
                    }
                    m_writer.WriteFinalHeader();
                    m_writer.DisconnectEncoder();
                    outputByteCount = (int)m_writer.WrittenSize; //this should never get to "long" dimensions...
                }

                return (testedByteCount - outputByteCount) * 1.0 / testedByteCount;
            }
            catch (UnauthorizedAccessException)
            {
                //exact error doesn't really matter - just important to know there was an issue
                testedByteCount = 0;
                outputByteCount = 0;
                return null;
            }
            catch (IOException)
            {
                //exact error doesn't really matter - just important to know there was an issue
                testedByteCount = 0;
                outputByteCount = 0;
                return null;
            }
        }
        public void Dispose()
        {
            if (m_archive != null)
            {
                m_archive = null;
            }

            if (m_reader != null)
            {
                try { m_reader.Close(); }
                finally { m_reader = null; }
            }

            if (m_writer != null)
            {
                try
                {
                    if (m_copyEncoder != null)
                    {
                        try
                        {
                            m_writer.ConnectEncoder(m_copyEncoder);
                            m_copyEncoder.Dispose();
                        }
                        finally { m_copyEncoder = null; }
                    }

                    if (m_lzma2Encoder != null)
                    {
                        try
                        {
                            m_writer.ConnectEncoder(m_lzma2Encoder);
                            m_lzma2Encoder.Dispose();
                        }
                        finally { m_lzma2Encoder = null; }
                    }

                    m_writer.WriteFinalHeader();
                }
                finally { m_writer = null; }
            }

            if (m_stream != null)
            {
                try { m_stream.Dispose(); }
                finally { m_stream = null; }
            }
        }