Decompress multiple files using 7z SDK

Decompress multiple files using 7z SDK



I am trying to implement the 7z SDK to compress and decompress multiple files at once.



I tried with one file at a time and both methods worked, now I implemented some changes on the compress method to support multiple files being compressed.


public static void CompressFile(List<string> inFiles, string outFile)

SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();

FileStream output = new FileStream(outFile, FileMode.Create);

// Write the encoder properties
coder.WriteCoderProperties(output);

int listSize = inFiles.Count;

// get the size of my list to loop through all the items
// Writing each file on the compressed file at a time

for(int i = 0; i < listSize; i++)

FileStream input = new FileStream(inFiles[i], FileMode.Open);

// Write the decompressed file size.
output.Write(BitConverter.GetBytes(input.Length), 0, 8);

// Encode the file.
coder.Code(input, output, input.Length, -1, null);

output.Flush();
output.Close();



I got a compressed file as expected but I need to implement the decompress method to test if everything went well.



I got stuck on figuring out how to implement the changes I need to decompress multiple files:


public static void DecompressFile(string inFile, string outFile)

//Original method used to decompress one file worked.

SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();


FileStream input = new FileStream(inFile, FileMode.Open);
FileStream output = new FileStream(outFile, FileMode.Create);

// Read the decoder properties
byte properties = new byte[5];
input.Read(properties, 0, 5);

// Read in the decompress file size.
byte fileLengthBytes = new byte[8];
input.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

coder.SetDecoderProperties(properties);
coder.Code(input, output, input.Length, fileLength, null);
output.Flush();
output.Close();




I had the idea (dont know if good or not) to compress all the files using a loop structure, but to decompress it I cant.
Which approach would be the ideal to decompress a file that is supposed to contain several files?




1 Answer
1



I did something like this with gzipstream. but its all about how you want pack the files.



In this case something like this would be ideal.


string payload = Path.GetTempFileName();
using (FileStream temp_fs = new FileStream(payload, FileMode.OpenOrCreate))

using (BinaryWriter output_s = new BinaryWriter(new FileStream("target.out", FileMode.OpenOrCreate)))

//Write a blank. must be a long!
output_s.Write((long)0);
foreach (string file in Files)


//Write the files name
output_s.Write(file);
long start = temp_fs.Position;

//Write the starting point
output_s.Write(start);

//Compress the file to the payload
using (GZipStream gzip = new GZipStream(temp_fs, CompressionMode.Compress, true))

using (FileStream fs = new FileStream(file, FileMode.Open))

fs.CopyTo(gzip);



//Write the length
output_s.Write(temp_fs.Position - start);


//When all files are written
//Get the size of our header
long headersize = output_s.BaseStream.Length - 8;

//Copy the temp file data to the end
temp_fs.CopyTo(output_s.BaseStream);

//Reset to the start of the stream
output_s.BaseStream.Position = 0;

//override our zero
output_s.Write(headersize);



File.Delete(payload);



When you read your file will a binary read you will be able to grab the file by name then the next to long after the filename will be its size and start.
then you would decompress section header+pos to length. to retrieve your file.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế