Exemple #1
0
        public static void Pack(string directory, string filename)
        {
            List<FileInfo> infos = new List<FileInfo>();
            LzmaDecoder decoder = new LzmaDecoder();

            // Figure out the info for each file we need to compress.
            foreach (string fn in Directory.GetFiles(directory, "*.lzma")) {
                // Remove the extension.
                infos.Add(new FileInfo(fn));
            }

            string tempFilename = Path.GetTempFileName();
            using (var output = File.OpenWrite(filename)) {
                // First pass: write file table to output, and combine files.
                //
                // File table format:
                // <number entries>
                // file1: <name> <lzma size> <uncompressed size>
                // file2: <name> <lzma size> <uncompressed size>
                // ......
                // filen: <name> <lzma size> <uncompressed size>
                using (BinaryWriter writer = new BinaryWriter(output)) {
                    writer.Write(infos.Count);
                    using (var tempFile = File.OpenWrite(tempFilename))
                        foreach (FileInfo fi in infos) {
                            writer.Write(fi.Name);
                            writer.Write(fi.Length);

                            // Decompress this file to the temp blob.
                            using (var thisFile = fi.OpenRead()) {
                                long uncompressedSize = PrepareDecoder(decoder, thisFile);
                                decoder.Code(thisFile, tempFile, thisFile.Length, uncompressedSize, null);

                                writer.Write(uncompressedSize);

                                thisFile.Pipe(tempFile);
                            }
                        }

                    // Second pass: write LZMA header to output, and compress the temp blob
                    // into the output file.
                    LzmaEncoder encoder = new LzmaEncoder();
                    using (var tempFile = File.OpenRead(tempFilename)) {
                        WriteHeader(encoder, tempFile, output);
                        encoder.Code(tempFile, output, -1, -1, null);
                    }
                }
            }
        }
        private void WriteCompressedFile(Image screenshot)
        {
            LzmaEncoder encoder = new LzmaEncoder();
            using (MemoryStream buffer = new MemoryStream()) {
                _params.Writer.Write(screenshot, buffer);

                // Rewind stream
                buffer.Seek(0, SeekOrigin.Begin);

                // LZMA encode from memory to file
                using (var output = File.OpenWrite(CreateFilename())) {
                    Lzma.WriteHeader(encoder, buffer, output);
                    encoder.Code(buffer, output, -1, -1, null);
                }
            }
        }
Exemple #3
0
 public static void WriteHeader(LzmaEncoder encoder, Stream input, Stream output)
 {
     encoder.WriteCoderProperties(output);
     output.Write(BitConverter.GetBytes(input.Length), 0, sizeof(Int64));
 }