Restore CZipOutputStream from MP3 prototype

Former-commit-id: 529c359935
This commit is contained in:
2022-12-05 21:30:20 -08:00
parent 914ef4a37c
commit 1e7e5887d2
4 changed files with 83 additions and 4 deletions

View File

@@ -0,0 +1,53 @@
#include "Kyoto/Streams/CZipOutputStream.hpp"
#include "rstl/math.hpp"
#include "Kyoto/Streams/CZipSupport.hpp"
CZipOutputStream::CZipOutputStream(COutputStream* out, int level)
: COutputStream(1024)
, mOutput(out)
, mCompressedBytesWritten(0)
, mZStream(new z_stream)
, mUnk(0) {
mZStream->zalloc = CZipSupport::Alloc;
mZStream->zfree = CZipSupport::Free;
mZStream->opaque = nullptr;
int useLevel = 9;
if (level < 10) {
useLevel = level;
}
deflateInit(mZStream.get(), level);
}
CZipOutputStream::~CZipOutputStream() {
DoFlush();
while (!Process(true))
;
}
void CZipOutputStream::Write(const void* buf, size_t len) {
mZStream->next_in = (Bytef*)buf;
mZStream->avail_in = len;
while (mZStream->avail_in != 0) {
Process(false);
}
}
bool CZipOutputStream::Process(bool flush) {
Bytef outBuf[1024];
mZStream->avail_out = 1024;
mZStream->next_out = outBuf;
int ret = deflate(mZStream.get(), flush);
/* assertion failure logic */
if (mZStream->avail_out != 1024) {
mOutput->DoPut(outBuf, 1024 - mZStream->avail_out);
mCompressedBytesWritten += 1024 - mZStream->avail_out;
}
return ret == Z_STREAM_END;
}