Exemple #1
0
 public Writer()
 {
     ms = new MemoryStream();
     ls = LZ4Stream.Encode(ms);
     bs = new BufferedStream(ls, BUFFER_SIZE);
     bw = new BinaryWriter(bs);
 }
Exemple #2
0
        public static void Encode(
            string original, string encoded, int chuckSize, LZ4Settings settings)
        {
            var frameInfo = new LZ4Descriptor(
                null, false, settings.Chaining, false, null, settings.BlockSize);

            using (var input = File.OpenRead(original))
                using (var output = File.Create(encoded))
                    using (var encode = new LZ4EncoderStream(
                               output, frameInfo, i => LZ4Encoder.Create(
                                   i.Chaining, settings.Level, i.BlockSize, settings.ExtraBlocks)))
                    {
                        var buffer = new byte[chuckSize];
                        while (true)
                        {
                            var read = input.Read(buffer, 0, buffer.Length);
                            if (read == 0)
                            {
                                break;
                            }

                            encode.Write(buffer, 0, read);
                        }
                    }
        }
        // Waits on the old write task, then resets the type to start recoding at the beginning of the buffer
        // This will return the compressed size of the previous item, or zero if it was not compressed
        internal uint Reset(string path, bool compress)
        {
            uint usize = _writeTask?.Result ?? 0;             // The "Result" property blocks until the task is finished

            _memStream.Seek(0, SeekOrigin.Begin);
            OutputSize   = 0;
            _currentFile = path;
            Compress     = compress;

            // Create the new streams (if there is a file)
            if (path != null)
            {
                _file = File.Open(_currentFile, FileMode.Create, FileAccess.Write, FileShare.None);
                if (Compress)
                {
                    _compressor = LZ4Stream.Encode(_file, LZ4Level.L00_FAST, leaveOpen: true);
                }
                else
                {
                    _compressor = null;
                }
            }
            else
            {
                _file       = null;
                _compressor = null;
            }

            return(usize);
        }
Exemple #4
0
        /// <inheritdoc />
        public byte[] Compress(byte[] source)
        {
            var output = new MemoryStream();

            using (LZ4EncoderStream stream = LZ4Stream.Encode(output))
            {
                var reader = new MemoryStream(source);
                reader.CopyTo(stream);
                stream.Close();
                return(output.ToArray());
            }
        }
        //starts new block and closes the old one
        private void startNewBlock()
        {
            closeBlock();

            //write "decompressed file byte offset" for the new block
            byte[] buffer = BitConverter.GetBytes(this.totalDecompressedByteCount);
            this.fileStream.Write(buffer, 0, 8);

            //write dummy for "compressed block size"
            for (int i = 0; i < 8; i++)
            {
                this.fileStream.WriteByte(0);
            }

            //reset block values an memory stream
            this.decompressedByteCountWithinBlock = 0;
            this.mStream           = new MemoryStream();
            this.compressionStream = LZ4Stream.Encode(this.mStream, this.encoderSettings, true);
        }
        public LZ4BlockStream(System.IO.FileStream filestream, AccessMode mode)
        {
            this.encoderSettings.CompressionLevel = K4os.Compression.LZ4.LZ4Level.L00_FAST;
            this.fileStream = filestream;
            this.mode       = mode;
            this.mStream    = new MemoryStream((int)this.decompressedBlockSize);

            //if "write-mode" then create new file and fill the first 2 X 8 header bytes
            if (this.mode == AccessMode.write)
            {
                this.fileStream.Seek(0, SeekOrigin.Begin);

                //we don't know "decompressed file size" yet, fill it with zeroes
                for (int i = 0; i < 8; i++)
                {
                    this.fileStream.WriteByte(0);
                }

                //write  "decompressed block size"
                byte[] blockSizeBytes = BitConverter.GetBytes(this.decompressedBlockSize);
                this.fileStream.Write(blockSizeBytes, 0, 8);

                //write first block dummies
                for (int i = 0; i < 16; i++)
                {
                    this.fileStream.WriteByte(0);
                }

                //open compression stream
                this.compressionStream = LZ4Stream.Encode(this.mStream, this.encoderSettings, true);
            }
            else if (this.mode == AccessMode.read)  //if read mode: read file header
            {
                byte[] buffer = new byte[8];
                this.fileStream.Read(buffer, 0, 8);
                this.decompressedFileSize = BitConverter.ToUInt64(buffer, 0);

                this.fileStream.Read(buffer, 0, 8);
                this.decompressedBlockSize = BitConverter.ToUInt64(buffer, 0);
            }
        }
        public static void PackImaginaryObjectToFile(string path, ImaginaryObject toStore)
        {
            Encoding encoding = Encoding.UTF8;

            Utilities.CreateEmptyFile(path);

            using (var fileStream = new FileStream(path, FileMode.Create))
#if DEBUG
                using (var writer = new BinaryWriter(fileStream, encoding))
#else
                using (LZ4EncoderStream compressionStream = LZ4Stream.Encode(fileStream))
                    using (BinaryWriter writer = new BinaryWriter(compressionStream, encoding))
#endif
                {
                    // Write the current CrystalClear version to the file.
                    writer.Write(CrystalClearInformation.CrystalClearVersion.ToString());

                    // Write the constructor data.
                    toStore.WriteThis(writer);

                    // Save the file.
                    writer.Flush();
                }
        }
Exemple #8
0
        private async Task Backup()
        {
            StatusBarVm.statusBarViewModel.Reset();
            await Task.Run(() =>
            {
                DebugHelper.WriteLine("{0} {1} {2} {3}", "[FModel]", "[BackupMenuItemViewModel]", "[Create]", $"{_backupFileName} is about to be created");
                StatusBarVm.statusBarViewModel.Set($"{Properties.Settings.Default.PakPath}", Properties.Resources.Loading);

                using FileStream fileStream = new FileStream(_backupFilePath, FileMode.Create);
                using LZ4EncoderStream compressionStream = LZ4Stream.Encode(fileStream, LZ4Level.L00_FAST);
                using BinaryWriter writer = new BinaryWriter(compressionStream);
                foreach (PakFileReader pakFile in MenuItems.pakFiles.GetPakFileReaders())
                {
                    if (pakFile.Info.bEncryptedIndex && pakFile.AesKey == null)
                    {
                        continue;
                    }

                    if (!Globals.CachedPakFiles.ContainsKey(pakFile.FileName))
                    {
                        pakFile.ReadIndex(pakFile.AesKey);
                        Globals.CachedPakFiles[pakFile.FileName] = pakFile;
                        StatusBarVm.statusBarViewModel.Set(string.Format(Properties.Resources.MountedPakTo, pakFile.FileName, pakFile.MountPoint), Properties.Resources.Loading);
                    }

                    foreach (var entry in pakFile)
                    {
                        // uasset or umap or idk
                        writer.Write(entry.Value.Offset);
                        writer.Write(entry.Value.Size);
                        writer.Write(entry.Value.UncompressedSize);
                        writer.Write(entry.Value.Encrypted);
                        writer.Write(entry.Value.StructSize);
                        writer.Write(pakFile.MountPoint + entry.Value.Name);
                        writer.Write(entry.Value.CompressionMethodIndex);

                        // uexp
                        if (entry.Value.Uexp != null)
                        {
                            writer.Write(entry.Value.Uexp.Offset);
                            writer.Write(entry.Value.Uexp.Size);
                            writer.Write(entry.Value.Uexp.UncompressedSize);
                            writer.Write(entry.Value.Uexp.Encrypted);
                            writer.Write(entry.Value.Uexp.StructSize);
                            writer.Write(pakFile.MountPoint + entry.Value.Uexp.Name);
                            writer.Write(entry.Value.Uexp.CompressionMethodIndex);
                        }
                        // ubulk
                        if (entry.Value.Ubulk != null)
                        {
                            writer.Write(entry.Value.Ubulk.Offset);
                            writer.Write(entry.Value.Ubulk.Size);
                            writer.Write(entry.Value.Ubulk.UncompressedSize);
                            writer.Write(entry.Value.Ubulk.Encrypted);
                            writer.Write(entry.Value.Ubulk.StructSize);
                            writer.Write(pakFile.MountPoint + entry.Value.Ubulk.Name);
                            writer.Write(entry.Value.Ubulk.CompressionMethodIndex);
                        }
                    }
                }
            }).ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    Tasks.TaskCompleted(t.Exception);
                }
                else if (new FileInfo(_backupFilePath).Length > 0)
                {
                    DebugHelper.WriteLine("{0} {1} {2} {3}", "[FModel]", "[BackupMenuItemViewModel]", "[Create]", $"{_backupFileName} successfully created");
                    StatusBarVm.statusBarViewModel.Set(string.Format(Properties.Resources.CreateSuccess, _backupFileName), Properties.Resources.Success);
                    Globals.gNotifier.ShowCustomMessage(Properties.Resources.Success, string.Format(Properties.Resources.CreateSuccess, _backupFileName), "/FModel;component/Resources/check-circle.ico", _backupFilePath);
                }
                else
                {
                    File.Delete(_backupFilePath);
                    DebugHelper.WriteLine("{0} {1} {2} {3}", "[FModel]", "[BackupMenuItemViewModel]", "[Create]", $"{_backupFileName} is empty, hence deleted");
                    StatusBarVm.statusBarViewModel.Set(string.Format(Properties.Resources.CreateError, _backupFileName), Properties.Resources.Error);
                    Globals.gNotifier.ShowCustomMessage(Properties.Resources.Error, string.Format(Properties.Resources.CreateError, _backupFileName), "/FModel;component/Resources/alert.ico");
                }
            },
                            TaskScheduler.FromCurrentSynchronizationContext());
        }