开发者

How to view compressed file by decompressing only partially?

I have compressed file, which I want to view using something like "less" utility in unix. "less" provides preprocessing option, through which I can decompress the file. But, what if I want to partially decompress开发者_如何学C file(as part of preprocessing) and direct/pipe the output to less? This has advantage of decompressing only part of the file.

If there is any other way to view compressed file by partially decompressing please let me know.

Thanks, Rahul


You need to come up with your own compression format for this; all existing libraries (gzip, bzip2, lzma) are streaming but not chunked (i.e. you can't seek in the compressed stream).

One solution is to create a file like a ZIP archive: A stream of entries followed by a table of contents (TOC). In your case, the entries (= chunks) will all have the same size before compression.

Try 4KB chunk size; that should still give pretty good compression, a reasonable amount of overhead and quick decompression time per chunk. Write the compressed chunks into a file and record the start offset for each in the TOC.

When seeking in the stream, find the 4KB offset that you need, read the offset from the TOC, read the compressed chunk, and decompress it.

[EDIT] You need to create a small tool which can start decompressing anywhere in the archive and which just continues decompressing until the EOF is reached. But you probably want to know which part of the file less is currently displaying. There is no simple solution to that. less is designed to display a stream. It can't "seek" in that stream; instead less will create a buffer somewhere in which it can seek. If you want to avoid that (usually because the file is too big), you must write your own version of less.

Alternatively, have a look at the source code of less. Maybe you can replace the "buffer input" part of the code with something that can talk to your decoder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜