Search code examples
c#sdk7zip

7zip SDK: Reading/Writing properties. What is written and how to read it in human form?


I am using the 7zip SDK to compress and decompress files. Everything is working just fine except that when I decompress the files, (multiple files compressed in one file) I do not know their extension.

The file name and extension are written in the properties at the time of compression?

If not, how could I write those properties?

How do I read those specific properties to programmatically name the extracted files?

COMPRESSION METHOD

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

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

           //This is how I am writing the standard properties

DECOMPRESSION METHOD

            // Read properties
            byte[] propertyBytes = new byte[5];
            input.Read(propertyBytes, 0, 5);
            coder.SetDecoderProperties(propertyBytes);

            //File length
            byte[] fileLengthBytes = new byte[8];
            input.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            //Reading the properties and the size of the file.

So based on the decompression method what I understand is that the file properties are on the bytes 0-5.

Should the file name and extension be located there? How do I read it to name my file during the creating of the extracted file?

I just need to find the name of the file compressed and its format to when creating the fileStream for the output file, I can name it correctly.

Any ideas?


Solution

  • I figured that:

    I am saving the file name and extension in a variable like that:

            string nameOfFile = new FileInfo(filesToCompress[i]).Name;
    

    To write the file name as a property on my compressed file, I have to transform that into a byte array and use the fileStream to write it into the file:

                //write the file name
                byte[] filenameByts = Encoding.Default.GetBytes(nameOfFile);
                output.Write(filenameByts, 0, filenameByts.Length);