Search code examples
c++zlibxz

How can i read xz file without decompressing in c++?


I want to read xz file effectively, so i think uncompress is not a good choice.

Is there any methods i can use to read xzfile without decompressing in c++?

I know zlib is a great tool to read gz file, but it cant be used in xz file.

I found i can vim xz file, it shows good. but when i use cpp ifstream getline, it comes out mess code. Can anyone explain on this?


Solution

  • Compression is an invertible process of turning one sequence of bytes into another, hopefully shorter1. Decompression is the inverse of that process. So of course if you have an already compressed sequence of bytes then you have to decompress it in order to recover the content. There's no way around it and thus a performance hit is unavoidable. So the answer to

    Is there any methods i can use to read xzfile without decompressing in c++?

    is simply "no". C++ or not, doesn't matter.

    As for

    I found i can vim xz file, it shows good.

    Yes, because vim decompresses the file (presumably in memory) under the hood for you. It just doesn't tell you about it.


    1 fun fact: mathematics tells us that for every compression algorithm there exists an input such that the algorithm actually generates larger output. Compression algorithms are based on the fact that what we compress has some nice patterns inside, e.g. words. That's also why applying compression multiple times just doesn't (and will never) work.