示例#1
0
        /// <summary>
        /// Unpack the package
        /// </summary>
        public AppletManifest Unpack()
        {
            switch (this.Compression)
            {
            case "lzma":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (var dfs = new LZipStream(ms, SharpCompress.Compressors.CompressionMode.Decompress, true))
                        return(AppletManifest.Load(dfs));

            case "bzip2":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (var dfs = new BZip2Stream(ms, SharpCompress.Compressors.CompressionMode.Decompress, true))
                        return(AppletManifest.Load(dfs));

            case "gzip":
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (GZipStream dfs = new GZipStream(ms, CompressionMode.Decompress))
                        return(AppletManifest.Load(dfs));

            default:
                using (MemoryStream ms = new MemoryStream(this.Manifest))
                    using (DeflateStream dfs = new DeflateStream(ms, CompressionMode.Decompress))
                        return(AppletManifest.Load(dfs));
            }
        }
示例#2
0
        public static async Task <Chunk> FromStream(Plane plane, Stream chunkSource)
        {
            BZip2Stream  decompressor       = new BZip2Stream(chunkSource, CompressionMode.Decompress, true);
            StreamReader decompressedSource = new StreamReader(decompressor);

            Chunk loadedChunk = JsonConvert.DeserializeObject <Chunk>(
                await decompressedSource.ReadToEndAsync(),
                new JsonSerializerSettings()
            {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            }
                );

            loadedChunk.plane          = plane;
            loadedChunk.Location.Plane = plane;
            foreach (DrawnLine line in loadedChunk.lines)
            {
                foreach (LocationReference point in line.Points)
                {
                    point.Plane = plane;
                }
            }

            loadedChunk.OnChunkUpdate += plane.HandleChunkUpdate;

            return(loadedChunk);
        }
示例#3
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, CompressionType.None, options));
        }
示例#4
0
        public TarWriter(Stream destination, CompressionInfo compressionInfo)
            : base(ArchiveType.Tar)
        {
            if (!destination.CanWrite)
            {
                throw new ArgumentException("Tars require writable streams.");
            }
            switch (compressionInfo.Type)
            {
            case CompressionType.None:
                break;

            case CompressionType.BZip2:
            {
                destination = new BZip2Stream(destination, CompressionMode.Compress, false);
            }
            break;

            case CompressionType.GZip:
            {
                destination = new GZipStream(destination, CompressionMode.Compress, false);
            }
            break;

            default:
            {
                throw new InvalidFormatException("Tar does not support compression: " + compressionInfo.Type);
            }
            }
            InitalizeStream(destination, false);
        }
    static void Main(string[] args)
    {
        string base64;

        using (var reader = File.OpenText(args[0]))
        {
            // Skip the first line, which has some header information
            // TODO: Use it instead, to validate the rest of the data.
            reader.ReadLine();
            base64 = reader.ReadToEnd();
        }

        byte[] bytes = Convert.FromBase64String(base64);

        int startOfBody = FindStartOfBody(bytes);

        using (var input = new MemoryStream(bytes, startOfBody, bytes.Length - startOfBody))
        {
            using (var bzip2 = new BZip2Stream(input, SharpCompress.Compressors.CompressionMode.Decompress, true))
            {
                using (var output = File.OpenWrite(args[1]))
                {
                    bzip2.CopyTo(output);
                }
            }
        }
    }
示例#6
0
        /// <summary>
        /// Start generating the online.db file.
        /// </summary>
        public void Run()
        {
            using (var sqlite = getSqliteConnection())
                using (var mysql = getMySqlConnection())
                {
                    Console.WriteLine("Starting generator...");

                    createSchema(sqlite);

                    Console.WriteLine("Created schema.");
                    copyBeatmaps(mysql, sqlite);

                    Console.WriteLine("Compressing...");

                    using (var inStream = File.OpenRead(sqliteFilePath))
                        using (var outStream = File.OpenWrite(sqliteBz2FilePath))
                            using (var bz2 = new BZip2Stream(outStream, CompressionMode.Compress, false))
                                inStream.CopyTo(bz2);

                    if (Environment.GetEnvironmentVariable("S3_KEY") != null)
                    {
                        Console.WriteLine("Uploading to S3...");

                        using (var stream = File.OpenRead(sqliteBz2FilePath))
                            Upload("assets.ppy.sh", "client-resources/online.db.bz2", stream, stream.Length, "application/x-bzip2");
                    }
                }

            Console.WriteLine("All done!");
        }
        public TarWriter(Stream destination, TarWriterOptions options)
            : base(ArchiveType.Tar, options)
        {
            finalizeArchiveOnClose = options.FinalizeArchiveOnClose;

            if (!destination.CanWrite)
            {
                throw new ArgumentException("Tars require writable streams.");
            }
            switch (options.CompressionType)
            {
                case CompressionType.None:
                    break;
                case CompressionType.BZip2:
                {
                    destination = new BZip2Stream(destination, CompressionMode.Compress, true);
                }
                    break;
                case CompressionType.GZip:
                {
                    destination = new GZipStream(destination, CompressionMode.Compress, true);
                }
                    break;
                case CompressionType.LZip:
                {
                    destination = new LZipStream(destination, CompressionMode.Compress, true);
                }
                    break;
                default:
                {
                    throw new InvalidFormatException("Tar does not support compression: " + options.CompressionType);
                }
            }
            InitalizeStream(destination);
        }
示例#8
0
        public static TarReader Open(Stream stream, Options options)
        {
            Utility.CheckNotNull(stream, "stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            if (GZipArchive.IsGZipFile(stream2))
            {
                stream2.Rewind(false);
                GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
                if (!TarArchive.IsTarFile(stream3))
                {
                    throw new InvalidFormatException("Not a tar file.");
                }
                stream2.Rewind(true);
                return(new TarReader(stream2, CompressionType.GZip, options));
            }
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
                if (!TarArchive.IsTarFile(stream4))
                {
                    throw new InvalidFormatException("Not a tar file.");
                }
                stream2.Rewind(true);
                return(new TarReader(stream2, CompressionType.BZip2, options));
            }
            stream2.Rewind(true);
            return(new TarReader(stream2, CompressionType.None, options));
        }
示例#9
0
 /// <summary>
 /// Decompresssed a block of data using the BZip2 algorithm.
 /// </summary>
 /// <param name="inData">The compressed data block.</param>
 /// <returns>The decompressed data.</returns>
 public static byte[] DecompressBZip2(byte[] inData)
 {
     using var ms               = new MemoryStream(inData);
     using var input            = new BZip2Stream(ms, CompressionMode.Decompress, false);
     using var decompressedData = new MemoryStream();
     input.CopyTo(decompressedData);
     return(decompressedData.ToArray());
 }
示例#10
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
            }

            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
        }
示例#11
0
        public override byte[] Compress(byte[] uncompressedData)
        {
            var outStream = new MemoryStream();

            using (var compress = new BZip2Stream(outStream, CompressionMode.Compress))
            {
                compress.Write(uncompressedData, 0, uncompressedData.Length);
            }
            return(outStream.ToArray());
        }
 /// <summary>
 /// Restore the configuration
 /// </summary>
 public SanteDBConfiguration Restore()
 {
     using (var lzs = new BZip2Stream(File.OpenRead(Path.ChangeExtension(this.m_configPath, "bak.bz2")), SharpCompress.Compressors.CompressionMode.Decompress, false))
     {
         var retVal = SanteDBConfiguration.Load(lzs);
         this.Save(retVal);
         ApplicationContext.Current.ConfigurationManager?.Reload();
         return(retVal);
     }
 }
示例#13
0
        public override byte[] Decompress(byte[] compressedData)
        {
            var inStream  = new MemoryStream(compressedData);
            var outStream = new MemoryStream();

            using (var decompress = new BZip2Stream(inStream, CompressionMode.Decompress))
            {
                CopyTo(decompress, outStream);
            }
            return(outStream.ToArray());
        }
示例#14
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, null))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, null, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            throw new InvalidOperationException("Cannot determine compressed stream type.");
        }
示例#15
0
        internal static MemoryStream CompressStream(Stream stream)
        {
            byte[] arr = new byte[stream.Length];
            stream.Read(arr, 0, (int)stream.Length);

            MemoryStream ms   = new MemoryStream();
            BZip2Stream  bzip = new BZip2Stream(ms, CompressionMode.Compress, true, false);

            bzip.Write(arr, 0, arr.Length);
            bzip.Close();

            return(ms);
        }
示例#16
0
        /// <summary>
        /// Saves this chunk to the specified stream.
        /// </summary>
        /// <param name="destination">The destination stream to save the chunk to.</param>
        public async Task SaveTo(Stream destination)
        {
            BZip2Stream  compressor = new BZip2Stream(destination, CompressionMode.Compress, true);
            StreamWriter destWriter = new StreamWriter(compressor)
            {
                AutoFlush = true
            };

            await destWriter.WriteLineAsync(JsonConvert.SerializeObject(this));

            compressor.Close();
            destination.Close();
        }
示例#17
0
        private void prepareLocalCache()
        {
            string cacheFilePath           = storage.GetFullPath(cache_database_name);
            string compressedCacheFilePath = $"{cacheFilePath}.bz2";

            cacheDownloadRequest = new FileWebRequest(compressedCacheFilePath, $"https://assets.ppy.sh/client-resources/{cache_database_name}.bz2?{DateTimeOffset.UtcNow:yyyyMMdd}");

            cacheDownloadRequest.Failed += ex =>
            {
                File.Delete(compressedCacheFilePath);
                File.Delete(cacheFilePath);

                Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache download failed: {ex}", LoggingTarget.Database);
            };

            cacheDownloadRequest.Finished += () =>
            {
                try
                {
                    using (var stream = File.OpenRead(cacheDownloadRequest.Filename))
                        using (var outStream = File.OpenWrite(cacheFilePath))
                            using (var bz2 = new BZip2Stream(stream, CompressionMode.Decompress, false))
                                bz2.CopyTo(outStream);

                    // set to null on completion to allow lookups to begin using the new source
                    cacheDownloadRequest = null;
                }
                catch (Exception ex)
                {
                    Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache extraction failed: {ex}", LoggingTarget.Database);
                    File.Delete(cacheFilePath);
                }
                finally
                {
                    File.Delete(compressedCacheFilePath);
                }
            };

            Task.Run(async() =>
            {
                try
                {
                    await cacheDownloadRequest.PerformAsync();
                }
                catch
                {
                    // Prevent throwing unobserved exceptions, as they will be logged from the network request to the log file anyway.
                }
            });
        }
示例#18
0
 /// <summary>
 /// Decompresssed a block of data using the BZip2 algorithm.
 /// </summary>
 /// <param name="inData">The compressed data block.</param>
 /// <returns>The decompressed data.</returns>
 public static byte[] DecompressBZip2(byte[] inData)
 {
     using (MemoryStream ms = new MemoryStream(inData))
     {
         using (BZip2Stream input = new BZip2Stream(ms, CompressionMode.Decompress))
         {
             using (MemoryStream decompressedData = new MemoryStream())
             {
                 input.CopyTo(decompressedData);
                 return(decompressedData.ToArray());
             }
         }
     }
 }
 /// <summary>
 /// Backup the configuration
 /// </summary>
 public void Backup(SanteDBConfiguration configuration)
 {
     try
     {
         // HACK: For some reason the DCG doesn't like to backup the configuration file
         using (var lzs = new BZip2Stream(File.Create(Path.ChangeExtension(this.m_configPath, "bak.bz2")), SharpCompress.Compressors.CompressionMode.Compress, false))
             configuration.Save(lzs);
     }
     catch (Exception e)
     {
         File.Delete(Path.ChangeExtension(this.m_configPath, "bak.bz2"));
         throw new InvalidOperationException($"Could not backup to {Path.ChangeExtension(this.m_configPath, "bak.bz2")}", e);
     }
 }
示例#20
0
        public static IReader Open(Stream stream, Options options)
        {
            Utility.CheckNotNull(stream, "stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            if (ZipArchive.IsZipFile(stream2, null))
            {
                stream2.Rewind(true);
                return(ZipReader.Open(stream2, null, options));
            }
            stream2.Rewind(false);
            if (GZipArchive.IsGZipFile(stream2))
            {
                stream2.Rewind(false);
                GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(stream3))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.GZip, options));
                }
                stream2.Rewind(true);
                return(GZipReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
                if (TarArchive.IsTarFile(stream4))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (TarArchive.IsTarFile(stream2))
            {
                stream2.Rewind(true);
                return(TarReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (!RarArchive.IsRarFile(stream2, options))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
            }
            stream2.Rewind(true);
            return(RarReader.Open(stream2, options));
        }
示例#21
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options ?? new ReaderOptions();
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, options, CompressionType.None));
        }
示例#22
0
        public static string DecompressString(byte[] byteArray)
        {
            MemoryStream ms   = new MemoryStream(byteArray);
            BZip2Stream  gzip = new BZip2Stream(ms, SharpCompress.Compressor.CompressionMode.Decompress, true);

            byte[]        buffer = StreamToByteArray(gzip);
            StringBuilder sb     = new StringBuilder();

            for (int i = 0; i < buffer.Length; i++)
            {
                sb.Append((char)buffer[i]);
            }
            gzip.Flush();
            gzip.Dispose();
            ms.Dispose();
            return(sb.ToString());
        }
示例#23
0
        internal static MemoryStream DecompressStream(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            BZip2Stream bzip = new BZip2Stream(stream, CompressionMode.Decompress, false, false);

            byte[]       buffer = new byte[16 * 1024];
            MemoryStream ms     = new MemoryStream();

            int read;

            while ((read = bzip.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }

            return(ms);
        }
示例#24
0
        /// <summary>
        /// Create an unsigned package
        /// </summary>
        /// <returns>The package.</returns>
        public AppletPackage CreatePackage(String compression = null)
        {
            AppletPackage retVal = new AppletPackage()
            {
                Meta        = this.Info,
                Compression = compression
            };

            using (MemoryStream ms = new MemoryStream())
            {
                Stream compressStream = null;
                try {
                    switch (compression)
                    {
                    case "lzma":
                        compressStream = new LZipStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "bzip2":
                        compressStream = new BZip2Stream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "gzip":
                        compressStream = new GZipStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;

                    case "none":
                        compressStream = ms;
                        break;

                    default:
                        compressStream = new DeflateStream(ms, SharpCompress.Compressors.CompressionMode.Compress, leaveOpen: true);
                        break;
                    }
                    XmlSerializer xsz = new XmlSerializer(typeof(AppletManifest));
                    xsz.Serialize(compressStream, this);
                }
                finally
                {
                    compressStream.Dispose();
                }
                retVal.Manifest = ms.ToArray();
            }
            return(retVal);
        }
        ///     Extracts an BZip2 file contained in fileEntry.
        /// </summary>
        /// <param name="fileEntry"> FileEntry to extract </param>
        /// <returns> Extracted files </returns>
        public IEnumerable <FileEntry> Extract(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor)
        {
            BZip2Stream?bzip2Stream = null;

            try
            {
                bzip2Stream = new BZip2Stream(fileEntry.Content, SharpCompress.Compressors.CompressionMode.Decompress, false);
                governor.CheckResourceGovernor(bzip2Stream.Length);
            }
            catch (Exception e)
            {
                Logger.Debug(Extractor.DEBUG_STRING, ArchiveFileType.BZIP2, fileEntry.FullPath, string.Empty, e.GetType());
            }
            if (bzip2Stream != null)
            {
                var newFilename = Path.GetFileNameWithoutExtension(fileEntry.Name);
                var entryStream = bzip2Stream.Length > options.MemoryStreamCutoff ?
                                  new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.DeleteOnClose) :
                                  (Stream) new MemoryStream((int)bzip2Stream.Length);
                var newFileEntry = new FileEntry(newFilename, bzip2Stream, fileEntry);

                if (Extractor.IsQuine(newFileEntry))
                {
                    Logger.Info(Extractor.IS_QUINE_STRING, fileEntry.Name, fileEntry.FullPath);
                    bzip2Stream.Dispose();
                    throw new OverflowException();
                }

                foreach (var extractedFile in Context.Extract(newFileEntry, options, governor))
                {
                    yield return(extractedFile);
                }
                bzip2Stream.Dispose();
            }
            else
            {
                if (options.ExtractSelfOnFail)
                {
                    yield return(fileEntry);
                }
            }
        }
示例#26
0
        /// <inheritdoc/>
        public ConcurrentDictionary <string, ConcurrentQueue <string> > Scan(Scanner scanner, Stream stream, string file)
        {
            // If the BZip2 file itself fails
            try
            {
                string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(tempPath);

                using (BZip2Stream bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
                {
                    // If an individual entry fails
                    try
                    {
                        string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
                        using (FileStream fs = File.OpenWrite(tempFile))
                        {
                            bz2File.CopyTo(fs);
                        }
                    }
                    catch { }
                }

                // Collect and format all found protections
                var protections = scanner.GetProtections(tempPath);

                // If temp directory cleanup fails
                try
                {
                    Directory.Delete(tempPath, true);
                }
                catch { }

                // Remove temporary path references
                Utilities.StripFromKeys(protections, tempPath);

                return(protections);
            }
            catch { }

            return(null);
        }
示例#27
0
            private void prepareLocalCache()
            {
                string cacheFilePath           = storage.GetFullPath(cache_database_name);
                string compressedCacheFilePath = $"{cacheFilePath}.bz2";

                cacheDownloadRequest = new FileWebRequest(compressedCacheFilePath, $"https://assets.ppy.sh/client-resources/{cache_database_name}.bz2");

                cacheDownloadRequest.Failed += ex =>
                {
                    File.Delete(compressedCacheFilePath);
                    File.Delete(cacheFilePath);

                    Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache download failed: {ex}", LoggingTarget.Database);
                };

                cacheDownloadRequest.Finished += () =>
                {
                    try
                    {
                        using (var stream = File.OpenRead(cacheDownloadRequest.Filename))
                            using (var outStream = File.OpenWrite(cacheFilePath))
                                using (var bz2 = new BZip2Stream(stream, CompressionMode.Decompress, false))
                                    bz2.CopyTo(outStream);

                        // set to null on completion to allow lookups to begin using the new source
                        cacheDownloadRequest = null;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache extraction failed: {ex}", LoggingTarget.Database);
                        File.Delete(cacheFilePath);
                    }
                    finally
                    {
                        File.Delete(compressedCacheFilePath);
                    }
                };

                cacheDownloadRequest.PerformAsync();
            }
        /// <summary>
        ///     Extracts an BZip2 file contained in fileEntry.
        /// </summary>
        /// <param name="fileEntry"> FileEntry to extract </param>
        /// <returns> Extracted files </returns>
        public async IAsyncEnumerable <FileEntry> ExtractAsync(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor)
        {
            BZip2Stream?bzip2Stream = null;

            try
            {
                bzip2Stream = new BZip2Stream(fileEntry.Content, SharpCompress.Compressors.CompressionMode.Decompress, false);
                governor.CheckResourceGovernor(bzip2Stream.Length);
            }
            catch (Exception e)
            {
                Logger.Debug(Extractor.DEBUG_STRING, ArchiveFileType.BZIP2, fileEntry.FullPath, string.Empty, e.GetType());
            }
            if (bzip2Stream != null)
            {
                var newFilename  = Path.GetFileNameWithoutExtension(fileEntry.Name);
                var newFileEntry = await FileEntry.FromStreamAsync(newFilename, bzip2Stream, fileEntry);

                if (Extractor.IsQuine(newFileEntry))
                {
                    Logger.Info(Extractor.IS_QUINE_STRING, fileEntry.Name, fileEntry.FullPath);
                    bzip2Stream.Dispose();
                    throw new OverflowException();
                }

                await foreach (var extractedFile in Context.ExtractAsync(newFileEntry, options, governor))
                {
                    yield return(extractedFile);
                }
                bzip2Stream.Dispose();
            }
            else
            {
                if (options.ExtractSelfOnFail)
                {
                    yield return(fileEntry);
                }
            }
        }
示例#29
0
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                if (finalizeArchiveOnClose) {
                    PadTo512(0, true);
                    PadTo512(0, true);
                }

                if(OutputStream.GetType()==typeof(BZip2Stream))

                {
                    BZip2Stream b =(BZip2Stream) OutputStream;
                    b.Finish();
                }
                else if(OutputStream.GetType() == typeof(LZipStream))

                {
                    LZipStream l =(LZipStream) OutputStream;
                    l.Finish();
                }
                //switch (OutputStream)
                //{
                //    case BZip2Stream b:
                //    {
                //        b.Finish();
                //        break;
                //    }
                //    case LZipStream l:
                //    {
                //        l.Finish();
                //        break;
                //    }
                //}
            }
            base.Dispose(isDisposing);
        }
示例#30
0
        public static IReader Open(Stream stream, Options options = 1)
        {
            stream.CheckNotNull("stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                if (TarArchive.IsTarFile(new BZip2Stream(stream2, CompressionMode.Decompress, false, false)))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (!TarArchive.IsTarFile(stream2))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.");
            }
            stream2.Rewind(true);
            return(TarReader.Open(stream2, options));
        }