26inline uint16 readUnalignedLittleEndianShort (
const void* buffer)
28 auto data = readUnaligned<uint16> (buffer);
32inline uint32 readUnalignedLittleEndianInt (
const void* buffer)
34 auto data = readUnaligned<uint32> (buffer);
38struct ZipFile::ZipEntryHolder
40 ZipEntryHolder (
const char* buffer,
int fileNameLen)
42 isCompressed = readUnalignedLittleEndianShort (buffer + 10) != 0;
43 entry.fileTime = parseFileTime (readUnalignedLittleEndianShort (buffer + 12),
44 readUnalignedLittleEndianShort (buffer + 14));
45 compressedSize = (int64) readUnalignedLittleEndianInt (buffer + 20);
46 entry.uncompressedSize = (int64) readUnalignedLittleEndianInt (buffer + 24);
47 streamOffset = (int64) readUnalignedLittleEndianInt (buffer + 42);
49 entry.externalFileAttributes = readUnalignedLittleEndianInt (buffer + 38);
50 auto fileType = (entry.externalFileAttributes >> 28) & 0xf;
51 entry.isSymbolicLink = (fileType == 0xA);
56 static Time parseFileTime (uint32 time, uint32 date)
noexcept
58 auto year = (int) (1980 + (date >> 9));
59 auto month = (int) (((date >> 5) & 15) - 1);
60 auto day = (int) (date & 31);
61 auto hours = (int) time >> 11;
62 auto minutes = (int) ((time >> 5) & 63);
63 auto seconds = (int) ((time & 31) << 1);
65 return { year, month, day, hours, minutes, seconds };
69 int64 streamOffset, compressedSize;
74static int64 findCentralDirectoryFileHeader (InputStream& input,
int& numEntries)
76 BufferedInputStream in (input, 8192);
78 in.setPosition (in.getTotalLength());
79 auto pos = in.getPosition();
80 auto lowestPos = jmax ((int64) 0, pos - 1048576);
83 while (pos > lowestPos)
85 in.setPosition (pos - 22);
86 pos = in.getPosition();
87 memcpy (buffer + 22, buffer, 4);
89 if (in.read (buffer, 22) != 22)
92 for (
int i = 0; i < 22; ++i)
94 if (readUnalignedLittleEndianInt (buffer + i) == 0x06054b50)
96 in.setPosition (pos + i);
98 numEntries = readUnalignedLittleEndianShort (buffer + 10);
99 auto offset = (int64) readUnalignedLittleEndianInt (buffer + 16);
103 in.setPosition (offset);
108 if (in.readInt() != 0x02014b50)
110 in.setPosition (offset - 4);
112 if (in.readInt() == 0x02014b50)
126struct ZipFile::ZipInputStream :
public InputStream
128 ZipInputStream (ZipFile& zf,
const ZipFile::ZipEntryHolder& zei)
130 zipEntryHolder (zei),
131 inputStream (zf.inputStream)
133 if (zf.inputSource !=
nullptr)
135 streamToDelete.reset (file.inputSource->createInputStream());
136 inputStream = streamToDelete.get();
141 zf.streamCounter.numOpenStreams++;
147 if (inputStream !=
nullptr
148 && inputStream->setPosition (zei.streamOffset)
149 && inputStream->read (buffer, 30) == 30
157 ~ZipInputStream()
override
160 if (inputStream !=
nullptr && inputStream == file.inputStream)
161 file.streamCounter.numOpenStreams--;
165 int64 getTotalLength()
override
167 return zipEntryHolder.compressedSize;
170 int read (
void* buffer,
int howMany)
override
175 howMany = (int) jmin ((int64) howMany, zipEntryHolder.compressedSize - pos);
177 if (inputStream ==
nullptr)
182 if (inputStream == file.inputStream)
184 const ScopedLock sl (file.lock);
185 inputStream->setPosition (pos + zipEntryHolder.streamOffset + headerSize);
186 num = inputStream->read (buffer, howMany);
190 inputStream->setPosition (pos + zipEntryHolder.streamOffset + headerSize);
191 num = inputStream->read (buffer, howMany);
198 bool isExhausted()
override
200 return headerSize <= 0 || pos >= zipEntryHolder.compressedSize;
203 int64 getPosition()
override
208 bool setPosition (int64 newPos)
override
210 pos = jlimit ((int64) 0, zipEntryHolder.compressedSize, newPos);
216 ZipEntryHolder zipEntryHolder;
219 InputStream* inputStream;
220 std::unique_ptr<InputStream> streamToDelete;
222 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ZipInputStream)
228 : inputStream (stream)
231 streamToDelete.reset (inputStream);
257ZipFile::OpenStreamCounter::~OpenStreamCounter()
271 return entries.
size();
276 if (
auto*
zei = entries[index])
277 return &(
zei->entry);
284 for (
int i = 0; i < entries.size(); ++i)
298 return getEntry (getIndexOfFileName (
fileName, ignoreCase));
305 if (
auto*
zei = entries[index])
307 stream =
new ZipInputStream (*
this, *
zei);
309 if (
zei->isCompressed)
312 GZIPDecompressorInputStream::deflateFormat,
313 zei->entry.uncompressedSize);
325 for (
int i = 0; i < entries.
size(); ++i)
334 std::sort (entries.
begin(), entries.
end(),
335 [] (
const ZipEntryHolder*
e1,
const ZipEntryHolder*
e2) { return e1->entry.filename < e2->entry.filename; });
341 std::unique_ptr<InputStream>
toDelete;
344 if (inputSource !=
nullptr)
346 in = inputSource->createInputStream();
353 auto centralDirectoryPos = findCentralDirectoryFileHeader (*in, numEntries);
355 if (centralDirectoryPos >= 0 && centralDirectoryPos < in->getTotalLength())
357 auto size = (size_t) (in->
getTotalLength() - centralDirectoryPos);
360 MemoryBlock headerData;
366 for (
int i = 0; i < numEntries; ++i)
371 auto* buffer =
static_cast<const char*
> (headerData.getData()) + pos;
372 auto fileNameLen = readUnalignedLittleEndianShort (buffer + 28u);
374 if (pos + 46 + fileNameLen > size)
377 entries.
add (
new ZipEntryHolder (buffer, fileNameLen));
379 pos += 46u + fileNameLen
380 + readUnalignedLittleEndianShort (buffer + 30u)
381 + readUnalignedLittleEndianShort (buffer + 32u);
391 for (
int i = 0; i < entries.
size(); ++i)
409 auto entryPath =
zei->entry.filename.replaceCharacter (
'\\',
'/');
418 return targetFile.createDirectory();
423 return Result::fail (
"Failed to open the zip file for reading");
425 if (targetFile.exists())
430 if (! targetFile.deleteFile())
431 return Result::fail (
"Failed to write to target file: " + targetFile.getFullPathName());
434 if (! targetFile.getParentDirectory().createDirectory())
435 return Result::fail (
"Failed to create target folder: " + targetFile.getParentDirectory().getFullPathName());
437 if (
zei->entry.isSymbolicLink)
449 if (
out.failedToOpen())
450 return Result::fail (
"Failed to write to target file: " + targetFile.getFullPathName());
455 targetFile.setCreationTime (
zei->entry.fileTime);
456 targetFile.setLastModificationTime (
zei->entry.fileTime);
457 targetFile.setLastAccessTime (
zei->entry.fileTime);
464struct ZipFile::Builder::Item
467 : file (f), stream (s), storedPathname (
storedPath), fileTime (time), compressionLevel (compression)
469 symbolicLink = (file.exists() && file.isSymbolicLink());
472 bool writeData (OutputStream& target,
const int64 overallStartPosition)
474 MemoryOutputStream compressedData ((
size_t) file.getSize());
480 uncompressedSize = relativePath.length();
482 checksum = zlibNamespace::crc32 (0, (uint8_t*) relativePath.toRawUTF8(), (
unsigned int) uncompressedSize);
483 compressedData << relativePath;
485 else if (compressionLevel > 0)
487 GZIPCompressorOutputStream compressor (compressedData, compressionLevel,
488 GZIPCompressorOutputStream::windowBitsRaw);
489 if (! writeSource (compressor))
494 if (! writeSource (compressedData))
498 compressedSize = (int64) compressedData.getDataSize();
499 headerStart = target.getPosition() - overallStartPosition;
501 target.writeInt (0x04034b50);
502 writeFlagsAndSizes (target);
503 target << storedPathname
509 bool writeDirectoryEntry (OutputStream& target)
511 target.writeInt (0x02014b50);
512 target.writeShort (symbolicLink ? 0x0314 : 0x0014);
513 writeFlagsAndSizes (target);
514 target.writeShort (0);
515 target.writeShort (0);
516 target.writeShort (0);
517 target.writeInt ((
int) (symbolicLink ? 0xA1ED0000 : 0));
518 target.writeInt ((
int) (uint32) headerStart);
519 target << storedPathname;
526 std::unique_ptr<InputStream> stream;
527 String storedPathname;
529 int64 compressedSize = 0, uncompressedSize = 0, headerStart = 0;
530 int compressionLevel = 0;
531 unsigned long checksum = 0;
532 bool symbolicLink =
false;
534 static void writeTimeAndDate (OutputStream& target, Time t)
536 target.writeShort ((
short) (t.getSeconds() + (t.getMinutes() << 5) + (t.getHours() << 11)));
537 target.writeShort ((
short) (t.getDayOfMonth() + ((t.getMonth() + 1) << 5) + ((t.getYear() - 1980) << 9)));
540 bool writeSource (OutputStream& target)
542 if (stream ==
nullptr)
544 stream.reset (file.createInputStream());
546 if (stream ==
nullptr)
551 uncompressedSize = 0;
552 const int bufferSize = 4096;
553 HeapBlock<unsigned char> buffer (bufferSize);
555 while (! stream->isExhausted())
557 auto bytesRead = stream->read (buffer, bufferSize);
562 checksum = zlibNamespace::crc32 (checksum, buffer, (
unsigned int) bytesRead);
563 target.write (buffer, (
size_t) bytesRead);
564 uncompressedSize += bytesRead;
571 void writeFlagsAndSizes (OutputStream& target)
const
573 target.writeShort (10);
574 target.writeShort ((
short) (1 << 11));
575 target.writeShort ((! symbolicLink && compressionLevel > 0) ? (
short) 8 : (short) 0);
576 writeTimeAndDate (target, fileTime);
577 target.writeInt ((
int) checksum);
578 target.writeInt ((
int) (uint32) compressedSize);
579 target.writeInt ((
int) (uint32) uncompressedSize);
580 target.writeShort ((
short) storedPathname.toUTF8().sizeInBytes() - 1);
581 target.writeShort (0);
584 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Item)
593 items.
add (
new Item (file,
nullptr, compression,
600 jassert (stream !=
nullptr);
602 items.add (
new Item ({}, stream, compression, path, time));
609 for (
int i = 0; i < items.size(); ++i)
611 if (progress !=
nullptr)
612 *progress = (i + 0.5) / items.
size();
614 if (! items.getUnchecked (i)->writeData (target,
fileStart))
620 for (
auto* item : items)
621 if (! item->writeDirectoryEntry (target))
635 if (progress !=
nullptr)
649 :
UnitTest (
"ZIP", UnitTestCategories::compression)
652 void runTest()
override
658 HashMap<String, MemoryBlock> blocks;
660 for (
auto& entryName : entryNames)
663 MemoryOutputStream mo (block,
false);
666 builder.
addEntry (
new MemoryInputStream (block,
false), 9, entryName, Time::getCurrentTime());
670 MemoryOutputStream mo (data,
false);
672 MemoryInputStream mi (data,
false);
676 expectEquals (zip.getNumEntries(), entryNames.size());
678 for (
auto& entryName : entryNames)
680 auto* entry = zip.getEntry (entryName);
681 std::unique_ptr<InputStream> input (zip.createStreamForEntry (*entry));
682 expectEquals (input->readEntireStreamAsString(), entryName);
687static ZIPTests zipTests;
ElementType getUnchecked(int index) const
bool isEmpty() const noexcept
int size() const noexcept
void add(const ElementType &newElement)
ElementType & getReference(int index) noexcept
static JUCE_CONSTEXPR uint16 littleEndianShort(const void *bytes) noexcept
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
Time getLastModificationTime() const
String getFileName() const
bool createSymbolicLink(const File &linkFileToCreate, bool overwriteExisting) const
static juce_wchar getSeparatorChar()
virtual int64 getPosition()=0
virtual bool writeShort(short value)
virtual bool writeInt(int value)
int size() const noexcept
ObjectClass * getUnchecked(int index) const noexcept
void clear(bool deleteObjects=true)
ObjectClass * add(ObjectClass *newObject)
ObjectClass ** begin() noexcept
ObjectClass ** end() noexcept
static Result fail(const String &errorMessage) noexcept
static Result ok() noexcept
bool isEmpty() const noexcept
static String fromUTF8(const char *utf8buffer, int bufferSizeBytes=-1)
bool isNotEmpty() const noexcept
void addEntry(InputStream *streamToRead, int compressionLevel, const String &storedPathName, Time fileModificationTime)
bool writeToStream(OutputStream &target, double *progress) const
void addFile(const File &fileToAdd, int compressionLevel, const String &storedPathName=String())
Result uncompressTo(const File &targetDirectory, bool shouldOverwriteFiles=true)
InputStream * createStreamForEntry(int index)
const ZipEntry * getEntry(int index) const noexcept
int getNumEntries() const noexcept
ZipFile(const File &file)
Result uncompressEntry(int index, const File &targetDirectory, bool shouldOverwriteFiles=true)
int getIndexOfFileName(const String &fileName, bool ignoreCase=false) const noexcept
void sortEntriesByFilename()