[{"data":1,"prerenderedAt":114},["ShallowReactive",2],{"/2025/10/16-lzma-sdk-compress-decompress":3},{"id":4,"title":5,"body":6,"date":105,"description":80,"extension":106,"meta":107,"navigation":108,"path":109,"robots":110,"seo":111,"stem":112,"__hash__":113},"posts/2025/10/16 LZMA-SDK-compress-decompress.md","16 LZMA SDK Compress Decompress",{"type":7,"value":8,"toc":100},"minimark",[9,18,25,28,51,53,61,64,67,81,83,89,92],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"lzma-sdk-compress-decompress","LZMA SDK Compress Decompress",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 2/27/2019.",[26,27],"br",{},[22,29,30,31,42,43,50],{},"7z is one of the best if not the best file compression available. Best of all, it is open source. The engine behind it is the LZMA compression method. I was integrating the sdk (",[32,33,36],"span",{"className":34},[35],"text-blue-600",[37,38,39],"a",{"href":39,"rel":40},"https://www.7-zip.org/sdk.html",[41],"nofollow",") in my project, but however, can't get a quick start on the compress decompress process. After searching the internet, I figured out on a surface level how it all works. Partially thanks to the question in ",[32,44,46],{"className":45},[35],[37,47,48],{"href":48,"rel":49},"https://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file",[41],". So, below, I write down my basic understanding.",[26,52],{},[54,55,56],"section-title",{},[57,58,60],"h2",{"id":59},"compress","Compress",[22,62,63],{},"Basically, the compressed file will contain 3 things with the first 2 are metadata:",[22,65,66],{},"The first 5 bytes are compression properties\nThe next 8 bytes are file size before compression\nThe compressed bytes",[68,69,70],"code-block",{},[71,72,77],"pre",{"className":73,"code":75,"language":76},[74],"language-text","var encoder = New Encoder();\nencoder.WriteCoderProperties(outStream); // Write properties\nencoder.Write(BitConverter.GetBytes(inputFileSize), 0, 8); // Write uncompressed file size\nencoder.Code(inStream, outSteam, inStream.Length, -1, null); // Actual compress\n","text",[78,79,75],"code",{"__ignoreMap":80},"",[26,82],{},[54,84,85],{},[57,86,88],{"id":87},"decompress","Decompress",[22,90,91],{},"To decompress the file, the metadata needs to be provided to the decoder. My code initially threw an error because there is no metadata.",[68,93,94],{},[71,95,98],{"className":96,"code":97,"language":76},[74],"var properties = new byte[5];\ninStream.Read(properties, 0, 5); // Read properties\n\nvar fileSizeBytes = new byte[8];\ninStream.Read(fileSizeBytes, 0, 8); // Read uncompressed file size\nvar fileSize = BitConverter.ToInt64(fileSizeBytes, 0);\n\nvar decoder = New Decoder();\ndecoder.SetDecoderProperties(properties); // Provide the properties to decoder\ndecoder.Code(inStream, outStream, inStream.Length, fileSize, null); // Actual decompress\n",[78,99,97],{"__ignoreMap":80},{"title":80,"searchDepth":101,"depth":101,"links":102},2,[103,104],{"id":59,"depth":101,"text":60},{"id":87,"depth":101,"text":88},"2025-10-16T00:00:00.000Z","md",{},true,"/2025/10/16-lzma-sdk-compress-decompress",null,{"title":5,"description":80},"2025/10/16 LZMA-SDK-compress-decompress","njDVwNnZZ6hHTLNjyBGVTCgMWqxovAdJgknuC0PaKJY",1785167452851]