Пример #1
0
        /// <summary>
        /// Loads the bzip2 block from the file
        /// </summary>
        /// <param name="beginning">The beginning of the block, in bits.</param>
        /// <param name="end">The end of the block, in bits.</param>
        /// <param name="buf">The buffer to load into.</param>
        /// <returns>The length of the loaded data, in bytes</returns>
        private long LoadBlock(long beginning, long end, ref byte[] buf)
        {
            long bufSize = buf.LongLength;

            bzip2.StatusCode status = bzip2.BZ2_bzLoadBlock(filePath, beginning, end, buf, ref bufSize);

            if (status != bzip2.StatusCode.BZ_OK)
            {
                throw new Exception(String.Format(Properties.Resources.FailedLoadingBlock, filePath, beginning, status));
            }

            // Just some initial value, we will reallocate the buffer as needed

            if (decompressionBuf == null)
            {
                decompressionBuf = new byte[buf.Length * 4];
            }

            int intBufSize    = (int)bufSize;
            int intDecompSize = decompressionBuf.Length;

            status = bzip2.BZ2_bzDecompress(buf, intBufSize, decompressionBuf, ref intDecompSize);

            // Limit a single uncompressed block size to 32 Mb

            while (status == bzip2.StatusCode.BZ_OUTBUFF_FULL &&
                   decompressionBuf.Length < 32000000)
            {
                decompressionBuf = new byte[decompressionBuf.Length * 2];

                intDecompSize = decompressionBuf.Length;

                status = bzip2.BZ2_bzDecompress(buf, intBufSize, decompressionBuf, ref intDecompSize);
            }

            if (decompressionBuf.Length > 32000000)
            {
                throw new Exception(String.Format(Properties.Resources.FailedUncompressingMemory, beginning));
            }

            if (status != bzip2.StatusCode.BZ_OK)
            {
                throw new Exception(String.Format(Properties.Resources.FailedUncompressingStatus, beginning, status));
            }

            // Exchange the raw buffer and the uncompressed one

            byte[] exch = buf;

            buf = decompressionBuf;

            decompressionBuf = exch;

            return(intDecompSize);
        }
Пример #2
0
        /// <summary>
        /// Locates the bzip2 blocks in the file
        /// </summary>
        private void LocateBlocks()
        {
            ReportProgress(0, IndexingProgress.State.Running, Properties.Resources.ProgressLocatingBlocks);
            FileInfo fi = new FileInfo(filePath);

            bz2_filesize     = fi.Length;
            startTime        = DateTime.Now;
            elapsed          = new TimeSpan(0);
            bzip2WatchThread = new Thread(BzipBlockLocatorProgressMonitor);
            bzip2WatchThread.Start(); // start the monitor, to report progress

            bzip2.StatusCode status = bzip2.BZ2_bzLocateBlocks(filePath, beginnings, ends, ref totalBlocks, ref bz2_blocks_pct_done);
            bzip2WatchThread.Abort();

            if (status != bzip2.StatusCode.BZ_OK)
            {
                throw new Exception(String.Format(Properties.Resources.FailedLocatingBlocks, filePath, status));
            }

            if (totalBlocks < 1)
            {
                throw new Exception(String.Format(Properties.Resources.NoBlocksFound, filePath));
            }
        }