Add support for TAR file handling in EPUB processing #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While I was reading Modern Software Engineering: Doing What Works to Build Better Software Faster by Dave Farley, I ran into a small but annoying issue that felt very familiar.
What happened
I was working with a bunch of
.epubfiles and, like most people, I assumed they would all follow the usual rule: an EPUB is just a ZIP file with a specific structure inside.Turns out that wasn’t always true.
Some of the files were actually TAR archives (sometimes compressed), just renamed to
.epub. As expected, standard EPUB libraries couldn’t read them at all.There were a couple of extra complications too:
What I ended up doing
Rather than special-casing everything later, I added a small pre-processing step to clean things up before reading the files:
Check the real file type
Instead of trusting the file extension, the code looks at the file signature to see what it actually is.
Convert in memory
If the file is a TAR, it gets converted to a ZIP on the fly, using in-memory buffers only.
Figure out the actual root
The required
mimetypefile is used as an anchor to find the real root of the EPUB, and any extra container directories are stripped out.Keep the rest simple
After that, the rest of the system always works with a normal ZIP-based EPUB, no matter how the file was originally packaged.
Nothing fancy, but it made the whole flow a bit more resilient and avoided a bunch of edge cases later on.