miniz icon indicating copy to clipboard operation
miniz copied to clipboard

Possibility to seek within iterative decompression

Open codecat opened this issue 2 years ago • 2 comments

I noticed that in #66, @unsightlygod implemented iterative decompression using the mz_zip_reader_extract_iter functions. Would it make sense or even be possible to add mz_zip_reader_extract_iter_seek to this, to seek within a file? Or would this not make sense in terms of compression?

Additionally, I noticed there is this unimplemented API, which looks to be pretty similar to the "iter" interface, which would be nice to have implemented.

#if 0
/* TODO */
	typedef void *mz_zip_streaming_extract_state_ptr;
	mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
	mz_uint64 mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
	mz_uint64 mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
	mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, mz_uint64 new_ofs);
	size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size);
	mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
#endif

codecat avatar May 06 '23 19:05 codecat

You can build seeking based on the existing mechanism... To seek forward an amount of bytes, read that amount, but discard the data. To seek backward, start iteration from the beginning, and seek forward.

Especially the latter is, of course, terribly inefficient. However, if it's possible to "clone" the iterator (I didn't try), this could be used to somewhat lessen the overhead of seeking backwards: the idea would be to "clone" the iterator at some points (maybe an interesting point in the file if you know the structure, maybe something like "every N megabytes"), and then "seek forward" again from one of these clones (ie the closest one before the desired position).

res2k avatar Sep 09 '23 17:09 res2k

I wonder if it might make sense to allow passing pVbuf=0 to the mz_zip_reader_extract_iter_read() function. It would then be able to act as a forward seek.

JCash avatar Nov 29 '24 09:11 JCash