mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Squashed 'src/beast/' changes from 43e6d34..0e7bac9
0e7bac9Fix include pathe5bb90fFix constness of Proxyac0142aUse template castef6e381Add missing Url.h include206e65cFix constness of operator[]695cc38Use template instantiation for friend declaration7b1e03aAdd BaseFromMember49bc04fMake List<>::Node not uncopyabled5954ffAdd Journal to UnitTest58da106Temporarily disable ServiceQueue dtor precondition assertsfe58c1aAdd missing #include2c02580Add PropertyStream for server state introspection24c2315Add ScopedWrapperContexta3845f5Add RelativeTime::value_type typedef7442932Fix missing PropertyStream membersed5a98fMore PropertyStream output for PeerFinderfcfa10dAdd PropertyStream3cf0729Tidy up AbstractHandler usage in HTTPClient55171f4Remove obsolete source files1311ca3Increase arity of SharedFunction67d807dAdd IPEndpoint::key_equalebf395eAdd ErrorCode and boost library2c3ead3Add ServiceQueue::wrap6c7f5d0Move many Thread related classes93e9d86Measure CPU utilization in ServiceQueueca47d72Move ServiceQueue, ThreadLocalValue, SpinLockc864e4dMove WaitableEventff305e6Add CPUMeter and ScopedTimeInterval01fd05cAdd RecursiveMutex, UnlockGuard, TryLockGuard5831a53Remove Journal from most Stoppable overridesb60a7f3Add Request and Response HTTP parsers44445ffRefactor net buffers classesac37c38Beast class refactor8b7056bFix eof on HTTP client get228b664Remove obsolete beast container classes1dfd655Use RelativeTime from startup in DeadlineTimerae22d5dAdd more methods to RelativeTimec67929eRemove unhandled exception catcher2472a90Add 64 bit output for MurmurHashf3d97c7Add RelativeTime::fromStartupb0b8660IPEndpoint better parsingae551cdAdd alternate form string parsing to IPEndpointd0a0dbfDon't break on Throw0e46762Add hasher functors for IPEndpointa1ec423Add Thread::stopThreadAsync4f7dca3Add compiler, stdlib, and platform skeleton to beast/config4394594Tidy up some use of Error for throwe5e0f52Journal console output improvementsf07515eAdd Stoppable prepare and start interfacesd37dd46Move RelativeTime to chrono, add ostream support3f6e7aaAdd console feature to Journalad0064aJournal option to write to Output window (MSVC)0b7574bAdd compilation test scriptcc05ce1Add ServiceQueuee132aabUse boost for functional when the config is set026b926Fix is_continuation for boost versionc807a4eFix invoked_type type reference2ff781bRemove LockFreeStack::size3acb474Add SharedData::ConstAccess7e4c834Add LockFreeStack::empty9c61a6dAdded AbstractHandler, WrapHandler. HTTPClient Fixes.94e40dcFix unittest, by removing recursive call.38bf408Fix nonstandard C++ extension in getNullSink1ef044dBuild fixesd5d3746Fix missing <cmath> include for Gentoo5f231d3Update copyright notice and licenses7b89bf6Add FixedArray, IntrusiveArray, Crypto5c5de57Reorganize beast modules and files9e18bb3Merge commit '43deaaa5cf0d0178a4a6c3cb69c02a2a9a43ec7d' as 'src/beast/beast/http/impl/http-parser'57703acFix BeforeBoost.h includefbc247bAdd Stoppable to beast56496d8IPEndpoint comparisons9d9c822Migrate some headers and general tidying1a3cddcAdd SharedArg and AsyncObject373ca9cAdd HTTPRequest and improvements to HTTPMessage parsing9534516Add some thread classes and fix SharedData with a simple mutex adapter755ab36Make CallQueue unit test runManualc0ca037Remove Beast version printing on startup7efb6a3Reorganize some MPL and Utility classes and files69c26a1Fix missing BeastConfig.h include in Net.cpp40aa552Disable Beast version printing in Ripple BeastConfig.h7b1352dAdd InterruptibleThread unit test68cf759ThreadWithCallQueue unit test adjustment6501deaIPEndpoint parsing and tidying72fc42bMove and add some template metaprogramming classes2a164f0Change filname capitalization (end)6a14f25Change filename capitalization92fd417Move integer types to beast/CStdInt.hebbd9ffMove TargetPlatform.h to beast/Config.h874b524Add IPEndpoint14b34fcTidy up some zlib macro undefines34fffcaRename beast sources for consistency4e59ab2Add CallQueue unit test327d7a6Fixes for consolidated beast unity includesd5ece4eRemove unused and broken classes39f13beRemove unused ConcurrentObject37624a7Add ThreadWithCallQueue unit teste82ec68Remove obsolete beast_Function90551a6Temporarily leave sqlite3 in whatever threading mode it was already in.43ebbb1Fix SharedSingleton to use memoryBarrierf343941Tidy up SharedSingleton doc comments001997eFix leak on exit from Singleton dependency cycle83b9d22Rename to DeadlineTimer::cancel()77874eeUse new instead of ::new for placement2a04dccJournal improvements50965caSharedFunction improvements277e32bAdd LockFreeStack iteratorsd94e4c2Fix undefined behavior in UnsignedIntegerCalc (again)2dc25ceFix DeadlineTimer, callback while holding lock207ffdeFix undefined behavior in UnsignedIntegerCalc1ad8ff9Fix UnsignedInteger::isZero1dd2836Add support for multiprecision integer arithmetic and binary data encodinga45fc47Update .gitignore962a95dTidy up UnsignedIntegerca695faAdd Time::isNull()e96ce99Better random number facilities in UnitTest550b8e5Fine tune UnsignedInteger declaration8e7e3b7Allow negative relative expirations in DeadlineTimerf3dc7ceAdd generic Journal class for loggingbfdda32Make ChildProcess UnitTest manual since it malfunctions02acf7dGeneral refactoring of beast framework classes84ef06eFix ExitHook to derive from AtExitHookf0acc9cReduce the max threads in the Workers unit test55447b0New SharedSingleton, resolves destruction of objects with static storage duration.41eb8a1Remove deprecated SharedPtr::getObject9eda4bcMake SharedObject members const, the counter mutable6eda777Remove deprecated createOnDemandOnce SingletonLifetime option8c522aaFix off by one in pending i/o count on HTTPClient057344eAdd HTTPMessage::toString and familyee728e3Add UniformResourceLocator::emptyae324fbMove ./modules to ./src git-subtree-dir: src/beast git-subtree-split:0e7bac945f
This commit is contained in:
595
modules/beast_core/zip/ZipFile.cpp
Normal file
595
modules/beast_core/zip/ZipFile.cpp
Normal file
@@ -0,0 +1,595 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the beast_core module of the BEAST library.
|
||||
Copyright (c) 2013 - Raw Material Software Ltd.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with
|
||||
or without fee is hereby granted, provided that the above copyright notice and this
|
||||
permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
|
||||
TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
|
||||
NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
||||
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
NOTE! This permissive ISC license applies ONLY to files within the beast_core module!
|
||||
All other BEAST modules are covered by a dual GPL/commercial license, so if you are
|
||||
using any other modules, be sure to check that you also comply with their license.
|
||||
|
||||
For more details, visit www.beast.com
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
class ZipFile::ZipEntryHolder
|
||||
{
|
||||
public:
|
||||
ZipEntryHolder (const char* const buffer, const int fileNameLen)
|
||||
{
|
||||
entry.filename = String::fromUTF8 (buffer + 46, fileNameLen);
|
||||
|
||||
const int time = ByteOrder::littleEndianShort (buffer + 12);
|
||||
const int date = ByteOrder::littleEndianShort (buffer + 14);
|
||||
entry.fileTime = getFileTimeFromRawEncodings (time, date);
|
||||
|
||||
compressed = ByteOrder::littleEndianShort (buffer + 10) != 0;
|
||||
compressedSize = (size_t) ByteOrder::littleEndianInt (buffer + 20);
|
||||
entry.uncompressedSize = ByteOrder::littleEndianInt (buffer + 24);
|
||||
|
||||
streamOffset = ByteOrder::littleEndianInt (buffer + 42);
|
||||
}
|
||||
|
||||
struct FileNameComparator
|
||||
{
|
||||
static int compareElements (const ZipEntryHolder* first, const ZipEntryHolder* second)
|
||||
{
|
||||
return first->entry.filename.compare (second->entry.filename);
|
||||
}
|
||||
};
|
||||
|
||||
ZipEntry entry;
|
||||
size_t streamOffset;
|
||||
size_t compressedSize;
|
||||
bool compressed;
|
||||
|
||||
private:
|
||||
static Time getFileTimeFromRawEncodings (int time, int date)
|
||||
{
|
||||
const int year = 1980 + (date >> 9);
|
||||
const int month = ((date >> 5) & 15) - 1;
|
||||
const int day = date & 31;
|
||||
const int hours = time >> 11;
|
||||
const int minutes = (time >> 5) & 63;
|
||||
const int seconds = (time & 31) << 1;
|
||||
|
||||
return Time (year, month, day, hours, minutes, seconds);
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
namespace
|
||||
{
|
||||
int findEndOfZipEntryTable (InputStream& input, int& numEntries)
|
||||
{
|
||||
BufferedInputStream in (input, 8192);
|
||||
|
||||
in.setPosition (in.getTotalLength());
|
||||
int64 pos = in.getPosition();
|
||||
const int64 lowestPos = bmax ((int64) 0, pos - 1024);
|
||||
|
||||
char buffer [32] = { 0 };
|
||||
|
||||
while (pos > lowestPos)
|
||||
{
|
||||
in.setPosition (pos - 22);
|
||||
pos = in.getPosition();
|
||||
memcpy (buffer + 22, buffer, 4);
|
||||
|
||||
if (in.read (buffer, 22) != 22)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < 22; ++i)
|
||||
{
|
||||
if (ByteOrder::littleEndianInt (buffer + i) == 0x06054b50)
|
||||
{
|
||||
in.setPosition (pos + i);
|
||||
in.read (buffer, 22);
|
||||
numEntries = ByteOrder::littleEndianShort (buffer + 10);
|
||||
|
||||
return (int) ByteOrder::littleEndianInt (buffer + 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
class ZipFile::ZipInputStream
|
||||
: public InputStream
|
||||
, LeakChecked <ZipInputStream>
|
||||
{
|
||||
public:
|
||||
ZipInputStream (ZipFile& zf, ZipFile::ZipEntryHolder& zei)
|
||||
: file (zf),
|
||||
zipEntryHolder (zei),
|
||||
pos (0),
|
||||
headerSize (0),
|
||||
inputStream (zf.inputStream)
|
||||
{
|
||||
if (zf.inputSource != nullptr)
|
||||
{
|
||||
inputStream = streamToDelete = file.inputSource->createInputStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
#if BEAST_DEBUG
|
||||
zf.streamCounter.numOpenStreams++;
|
||||
#endif
|
||||
}
|
||||
|
||||
char buffer [30];
|
||||
|
||||
if (inputStream != nullptr
|
||||
&& inputStream->setPosition (zei.streamOffset)
|
||||
&& inputStream->read (buffer, 30) == 30
|
||||
&& ByteOrder::littleEndianInt (buffer) == 0x04034b50)
|
||||
{
|
||||
headerSize = 30 + ByteOrder::littleEndianShort (buffer + 26)
|
||||
+ ByteOrder::littleEndianShort (buffer + 28);
|
||||
}
|
||||
}
|
||||
|
||||
~ZipInputStream()
|
||||
{
|
||||
#if BEAST_DEBUG
|
||||
if (inputStream != nullptr && inputStream == file.inputStream)
|
||||
file.streamCounter.numOpenStreams--;
|
||||
#endif
|
||||
}
|
||||
|
||||
int64 getTotalLength()
|
||||
{
|
||||
return zipEntryHolder.compressedSize;
|
||||
}
|
||||
|
||||
int read (void* buffer, int howMany)
|
||||
{
|
||||
if (headerSize <= 0)
|
||||
return 0;
|
||||
|
||||
howMany = (int) bmin ((int64) howMany, (int64) (zipEntryHolder.compressedSize - pos));
|
||||
|
||||
if (inputStream == nullptr)
|
||||
return 0;
|
||||
|
||||
int num;
|
||||
|
||||
if (inputStream == file.inputStream)
|
||||
{
|
||||
const ScopedLock sl (file.lock);
|
||||
inputStream->setPosition (pos + zipEntryHolder.streamOffset + headerSize);
|
||||
num = inputStream->read (buffer, howMany);
|
||||
}
|
||||
else
|
||||
{
|
||||
inputStream->setPosition (pos + zipEntryHolder.streamOffset + headerSize);
|
||||
num = inputStream->read (buffer, howMany);
|
||||
}
|
||||
|
||||
pos += num;
|
||||
return num;
|
||||
}
|
||||
|
||||
bool isExhausted()
|
||||
{
|
||||
return headerSize <= 0 || pos >= (int64) zipEntryHolder.compressedSize;
|
||||
}
|
||||
|
||||
int64 getPosition()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
bool setPosition (int64 newPos)
|
||||
{
|
||||
pos = blimit ((int64) 0, (int64) zipEntryHolder.compressedSize, newPos);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
ZipFile& file;
|
||||
ZipEntryHolder zipEntryHolder;
|
||||
int64 pos;
|
||||
int headerSize;
|
||||
InputStream* inputStream;
|
||||
ScopedPointer<InputStream> streamToDelete;
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
ZipFile::ZipFile (InputStream* const stream, const bool deleteStreamWhenDestroyed)
|
||||
: inputStream (stream)
|
||||
{
|
||||
if (deleteStreamWhenDestroyed)
|
||||
streamToDelete = inputStream;
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
ZipFile::ZipFile (InputStream& stream)
|
||||
: inputStream (&stream)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
ZipFile::ZipFile (const File& file)
|
||||
: inputStream (nullptr),
|
||||
inputSource (new FileInputSource (file))
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
ZipFile::ZipFile (InputSource* const source)
|
||||
: inputStream (nullptr),
|
||||
inputSource (source)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
ZipFile::~ZipFile()
|
||||
{
|
||||
entries.clear();
|
||||
}
|
||||
|
||||
#if BEAST_DEBUG
|
||||
ZipFile::OpenStreamCounter::~OpenStreamCounter()
|
||||
{
|
||||
/* If you hit this assertion, it means you've created a stream to read one of the items in the
|
||||
zipfile, but you've forgotten to delete that stream object before deleting the file..
|
||||
Streams can't be kept open after the file is deleted because they need to share the input
|
||||
stream that is managed by the ZipFile object.
|
||||
*/
|
||||
bassert (numOpenStreams == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
int ZipFile::getNumEntries() const noexcept
|
||||
{
|
||||
return entries.size();
|
||||
}
|
||||
|
||||
const ZipFile::ZipEntry* ZipFile::getEntry (const int index) const noexcept
|
||||
{
|
||||
if (ZipEntryHolder* const zei = entries [index])
|
||||
return &(zei->entry);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ZipFile::getIndexOfFileName (const String& fileName) const noexcept
|
||||
{
|
||||
for (int i = 0; i < entries.size(); ++i)
|
||||
if (entries.getUnchecked (i)->entry.filename == fileName)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
const ZipFile::ZipEntry* ZipFile::getEntry (const String& fileName) const noexcept
|
||||
{
|
||||
return getEntry (getIndexOfFileName (fileName));
|
||||
}
|
||||
|
||||
InputStream* ZipFile::createStreamForEntry (const int index)
|
||||
{
|
||||
InputStream* stream = nullptr;
|
||||
|
||||
if (ZipEntryHolder* const zei = entries[index])
|
||||
{
|
||||
stream = new ZipInputStream (*this, *zei);
|
||||
|
||||
if (zei->compressed)
|
||||
{
|
||||
stream = new GZIPDecompressorInputStream (stream, true, true,
|
||||
zei->entry.uncompressedSize);
|
||||
|
||||
// (much faster to unzip in big blocks using a buffer..)
|
||||
stream = new BufferedInputStream (stream, 32768, true);
|
||||
}
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
InputStream* ZipFile::createStreamForEntry (const ZipEntry& entry)
|
||||
{
|
||||
for (int i = 0; i < entries.size(); ++i)
|
||||
if (&entries.getUnchecked (i)->entry == &entry)
|
||||
return createStreamForEntry (i);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ZipFile::sortEntriesByFilename()
|
||||
{
|
||||
ZipEntryHolder::FileNameComparator sorter;
|
||||
entries.sort (sorter);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ZipFile::init()
|
||||
{
|
||||
ScopedPointer <InputStream> toDelete;
|
||||
InputStream* in = inputStream;
|
||||
|
||||
if (inputSource != nullptr)
|
||||
{
|
||||
in = inputSource->createInputStream();
|
||||
toDelete = in;
|
||||
}
|
||||
|
||||
if (in != nullptr)
|
||||
{
|
||||
int numEntries = 0;
|
||||
int pos = findEndOfZipEntryTable (*in, numEntries);
|
||||
|
||||
if (pos >= 0 && pos < in->getTotalLength())
|
||||
{
|
||||
const int size = (int) (in->getTotalLength() - pos);
|
||||
|
||||
in->setPosition (pos);
|
||||
MemoryBlock headerData;
|
||||
|
||||
if (in->readIntoMemoryBlock (headerData, size) == size)
|
||||
{
|
||||
pos = 0;
|
||||
|
||||
for (int i = 0; i < numEntries; ++i)
|
||||
{
|
||||
if (pos + 46 > size)
|
||||
break;
|
||||
|
||||
const char* const buffer = static_cast <const char*> (headerData.getData()) + pos;
|
||||
|
||||
const int fileNameLen = ByteOrder::littleEndianShort (buffer + 28);
|
||||
|
||||
if (pos + 46 + fileNameLen > size)
|
||||
break;
|
||||
|
||||
entries.add (new ZipEntryHolder (buffer, fileNameLen));
|
||||
|
||||
pos += 46 + fileNameLen
|
||||
+ ByteOrder::littleEndianShort (buffer + 30)
|
||||
+ ByteOrder::littleEndianShort (buffer + 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result ZipFile::uncompressTo (const File& targetDirectory,
|
||||
const bool shouldOverwriteFiles)
|
||||
{
|
||||
for (int i = 0; i < entries.size(); ++i)
|
||||
{
|
||||
Result result (uncompressEntry (i, targetDirectory, shouldOverwriteFiles));
|
||||
if (result.failed())
|
||||
return result;
|
||||
}
|
||||
|
||||
return Result::ok();
|
||||
}
|
||||
|
||||
Result ZipFile::uncompressEntry (const int index,
|
||||
const File& targetDirectory,
|
||||
bool shouldOverwriteFiles)
|
||||
{
|
||||
const ZipEntryHolder* zei = entries.getUnchecked (index);
|
||||
|
||||
#if BEAST_WINDOWS
|
||||
const String entryPath (zei->entry.filename);
|
||||
#else
|
||||
const String entryPath (zei->entry.filename.replaceCharacter ('\\', '/'));
|
||||
#endif
|
||||
|
||||
const File targetFile (targetDirectory.getChildFile (entryPath));
|
||||
|
||||
if (entryPath.endsWithChar ('/') || entryPath.endsWithChar ('\\'))
|
||||
return targetFile.createDirectory(); // (entry is a directory, not a file)
|
||||
|
||||
ScopedPointer<InputStream> in (createStreamForEntry (index));
|
||||
|
||||
if (in == nullptr)
|
||||
return Result::fail ("Failed to open the zip file for reading");
|
||||
|
||||
if (targetFile.exists())
|
||||
{
|
||||
if (! shouldOverwriteFiles)
|
||||
return Result::ok();
|
||||
|
||||
if (! targetFile.deleteFile())
|
||||
return Result::fail ("Failed to write to target file: " + targetFile.getFullPathName());
|
||||
}
|
||||
|
||||
if (! targetFile.getParentDirectory().createDirectory())
|
||||
return Result::fail ("Failed to create target folder: " + targetFile.getParentDirectory().getFullPathName());
|
||||
|
||||
{
|
||||
FileOutputStream out (targetFile);
|
||||
|
||||
if (out.failedToOpen())
|
||||
return Result::fail ("Failed to write to target file: " + targetFile.getFullPathName());
|
||||
|
||||
out << *in;
|
||||
}
|
||||
|
||||
targetFile.setCreationTime (zei->entry.fileTime);
|
||||
targetFile.setLastModificationTime (zei->entry.fileTime);
|
||||
targetFile.setLastAccessTime (zei->entry.fileTime);
|
||||
|
||||
return Result::ok();
|
||||
}
|
||||
|
||||
|
||||
//=============================================================================
|
||||
extern unsigned long beast_crc32 (unsigned long crc, const unsigned char*, unsigned len);
|
||||
|
||||
class ZipFile::Builder::Item : LeakChecked <ZipFile::Builder::Item>, public Uncopyable
|
||||
{
|
||||
public:
|
||||
Item (const File& f, const int compression, const String& storedPath)
|
||||
: file (f),
|
||||
storedPathname (storedPath.isEmpty() ? f.getFileName() : storedPath),
|
||||
compressionLevel (compression),
|
||||
compressedSize (0),
|
||||
headerStart (0)
|
||||
{
|
||||
}
|
||||
|
||||
bool writeData (OutputStream& target, const int64 overallStartPosition)
|
||||
{
|
||||
MemoryOutputStream compressedData;
|
||||
|
||||
if (compressionLevel > 0)
|
||||
{
|
||||
GZIPCompressorOutputStream compressor (&compressedData, compressionLevel, false,
|
||||
GZIPCompressorOutputStream::windowBitsRaw);
|
||||
if (! writeSource (compressor))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (! writeSource (compressedData))
|
||||
return false;
|
||||
}
|
||||
|
||||
compressedSize = (int) compressedData.getDataSize();
|
||||
headerStart = (int) (target.getPosition() - overallStartPosition);
|
||||
|
||||
target.writeInt (0x04034b50);
|
||||
writeFlagsAndSizes (target);
|
||||
target << storedPathname
|
||||
<< compressedData;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool writeDirectoryEntry (OutputStream& target)
|
||||
{
|
||||
target.writeInt (0x02014b50);
|
||||
target.writeShort (20); // version written
|
||||
writeFlagsAndSizes (target);
|
||||
target.writeShort (0); // comment length
|
||||
target.writeShort (0); // start disk num
|
||||
target.writeShort (0); // internal attributes
|
||||
target.writeInt (0); // external attributes
|
||||
target.writeInt (headerStart);
|
||||
target << storedPathname;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const File file;
|
||||
String storedPathname;
|
||||
int compressionLevel, compressedSize, headerStart;
|
||||
unsigned long checksum;
|
||||
|
||||
void writeTimeAndDate (OutputStream& target) const
|
||||
{
|
||||
const Time t (file.getLastModificationTime());
|
||||
target.writeShort ((short) (t.getSeconds() + (t.getMinutes() << 5) + (t.getHours() << 11)));
|
||||
target.writeShort ((short) (t.getDayOfMonth() + ((t.getMonth() + 1) << 5) + ((t.getYear() - 1980) << 9)));
|
||||
}
|
||||
|
||||
bool writeSource (OutputStream& target)
|
||||
{
|
||||
checksum = 0;
|
||||
FileInputStream input (file);
|
||||
|
||||
if (input.failedToOpen())
|
||||
return false;
|
||||
|
||||
const int bufferSize = 2048;
|
||||
HeapBlock<unsigned char> buffer (bufferSize);
|
||||
|
||||
while (! input.isExhausted())
|
||||
{
|
||||
const int bytesRead = input.read (buffer, bufferSize);
|
||||
|
||||
if (bytesRead < 0)
|
||||
return false;
|
||||
|
||||
checksum = beast_crc32 (checksum, buffer, (unsigned int) bytesRead);
|
||||
target.write (buffer, (size_t) bytesRead);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void writeFlagsAndSizes (OutputStream& target) const
|
||||
{
|
||||
target.writeShort (10); // version needed
|
||||
target.writeShort (0); // flags
|
||||
target.writeShort (compressionLevel > 0 ? (short) 8 : (short) 0);
|
||||
writeTimeAndDate (target);
|
||||
target.writeInt ((int) checksum);
|
||||
target.writeInt (compressedSize);
|
||||
target.writeInt ((int) file.getSize());
|
||||
target.writeShort ((short) storedPathname.toUTF8().sizeInBytes() - 1);
|
||||
target.writeShort (0); // extra field length
|
||||
}
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
ZipFile::Builder::Builder() {}
|
||||
ZipFile::Builder::~Builder() {}
|
||||
|
||||
void ZipFile::Builder::addFile (const File& fileToAdd, const int compressionLevel, const String& storedPathName)
|
||||
{
|
||||
items.add (new Item (fileToAdd, compressionLevel, storedPathName));
|
||||
}
|
||||
|
||||
bool ZipFile::Builder::writeToStream (OutputStream& target, double* const progress) const
|
||||
{
|
||||
const int64 fileStart = target.getPosition();
|
||||
|
||||
for (int i = 0; i < items.size(); ++i)
|
||||
{
|
||||
if (progress != nullptr)
|
||||
*progress = (i + 0.5) / items.size();
|
||||
|
||||
if (! items.getUnchecked (i)->writeData (target, fileStart))
|
||||
return false;
|
||||
}
|
||||
|
||||
const int64 directoryStart = target.getPosition();
|
||||
|
||||
for (int i = 0; i < items.size(); ++i)
|
||||
if (! items.getUnchecked (i)->writeDirectoryEntry (target))
|
||||
return false;
|
||||
|
||||
const int64 directoryEnd = target.getPosition();
|
||||
|
||||
target.writeInt (0x06054b50);
|
||||
target.writeShort (0);
|
||||
target.writeShort (0);
|
||||
target.writeShort ((short) items.size());
|
||||
target.writeShort ((short) items.size());
|
||||
target.writeInt ((int) (directoryEnd - directoryStart));
|
||||
target.writeInt ((int) (directoryStart - fileStart));
|
||||
target.writeShort (0);
|
||||
|
||||
if (progress != nullptr)
|
||||
*progress = 1.0;
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user