Esempio n. 1
0
 private static void ExtractFile(string f, LzmaDecoder decoder)
 {
     using (var input = File.OpenRead(f)) {
         long uncompressedLength = Lzma.PrepareDecoder(decoder, input);
         using (var output = File.OpenWrite(f.Remove(f.Length - 5)))
             decoder.Code(input, output, input.Length, uncompressedLength, null);
     }
 }
Esempio n. 2
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);
                    }
                }
            }
        }
Esempio n. 3
0
        public static void Unpack(string filename, string directory)
        {
            List<ExtendedFileInfo> infos = new List<ExtendedFileInfo>();
            string tempFilename = Path.GetTempFileName();

            using (var archive = File.OpenRead(filename)) {
                using (BinaryReader reader = new BinaryReader(archive)) {
                    int count = reader.ReadInt32();
                    for (int i = 0; i < count; i++) {
                        string name = reader.ReadString();
                        long csize = reader.ReadInt64();
                        long ucsize = reader.ReadInt64();
                        infos.Add(new ExtendedFileInfo(new FileInfo(Path.Combine(directory,name)), csize, ucsize));
                    }

                    // Unpack the compressed blob.
                    using (var tempFile = File.Open(tempFilename, FileMode.Create, FileAccess.ReadWrite)) {
                        LzmaDecoder decoder = new LzmaDecoder();
                        long uncompressedSize = PrepareDecoder(decoder, archive);
                        decoder.Code(archive, tempFile, archive.Length - archive.Position, uncompressedSize, null);

                        // Return to the beginning of the file.
                        tempFile.Seek(0, SeekOrigin.Begin);
                        // Split the compressed blob into individual files.
                        foreach (var efi in infos) {
                            using (var output = File.OpenWrite(efi.FileInfo.FullName.Remove(efi.FileInfo.FullName.Length - 5))) {
                                tempFile.Pipe(output, efi.UncompressedSize);
                            }
                        }
                    }
                }
            }
        }