Good day, dear Habr!
Over the past half a year, the crooked path of my pet projects has led me into such jungle from where it is still not possible to get out. And it all started harmlessly - a site with pictures, but a sense of perfectionism, the pursuit of a freebie, as well as some features of my mindset turned this, as originally planned, little walk, into a real long journey. Well, okay, as one rather burry revolutionary used to say: “Learn, study and study again”, and I, willy-nilly, have to follow this admonition.
Oh, something we were distracted from the main topic. I will no longer bore you with lengthy speeches, but I will get down to business.
Create a zip archive
In principle, I will not rewrite the
specification here. On the whole, it makes no sense to describe the structure either, because all this was
done before me .
For those who are too lazy to follow the links, I just outline briefly that any zip archive should contain:
- File entry:
- Local file header
- Useful data
- Data descriptor (optional, used when we do not know the file size and its hash until we read it to the end)
- Central Directory File Header (for each file. This is like a table of contents of the book, where each section is indicated and the page on which it can be found)
- End of central directory
Knowing this, we can try to write a simple archive that will contain only two files:
<?php
Try to run this primitive code and the output will give you a Lorem.zip file that will contain 1.txt and 2.txt.
What for?
Of course, any adequate person will say that writing archivers in php is a futile undertaking, especially since for a format such as zip, there are a bunch of ready-made implementations for every taste and color. And in the same php there are ready-made libraries. I will say so too :)
But why, then, is this whole article, why did I spend time writing it, and you reading it?
And then, that everything is not so simple and knowing how zip works opens up some additional possibilities.
Firstly, I hope, at least a little, but it will help those who want to understand the structure of zip.
And secondly, creating the archive with our own hands, we have control, and, most importantly, access to its internal data.
We can pre-calculate the Local File Header and Central Directory File Header, and then on-demand generate a zip archive on the fly with any content and order of files, simply substituting this data. And no overhead except for I / O.
Or, we can record the archive, upload it, for example, to the cloud, which supports fragmented downloading and, knowing the offsets for each of the files, get any of the archive files as if it weren’t in the archive at all, adding only one header to request. And then all this can be proxied and ...
Okay, let's not get ahead of ourselves. If you are interested in this topic, then in the following articles I will try to consider these opportunities and show how to use them.