From 74780131bd84759c1de6ec24fd896d78b6f7a9db Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jul 2022 10:05:17 +0200 Subject: [PATCH] lib/zlib/GzipOutputStream: add SyncFlush() --- src/lib/zlib/GzipOutputStream.cxx | 25 ++++++++++++++++++++++++- src/lib/zlib/GzipOutputStream.hxx | 7 ++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/lib/zlib/GzipOutputStream.cxx b/src/lib/zlib/GzipOutputStream.cxx index a72811ed7..48e0b8435 100644 --- a/src/lib/zlib/GzipOutputStream.cxx +++ b/src/lib/zlib/GzipOutputStream.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2018 Max Kellermann + * Copyright 2014-2021 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -54,6 +54,29 @@ GzipOutputStream::~GzipOutputStream() deflateEnd(&z); } +void +GzipOutputStream::SyncFlush() +{ + /* no more input */ + z.next_in = nullptr; + z.avail_in = 0; + + do { + Bytef output[16384]; + z.next_out = output; + z.avail_out = sizeof(output); + + int result = deflate(&z, Z_SYNC_FLUSH); + if (result != Z_OK) + throw ZlibError(result); + + if (z.next_out == output) + break; + + next.Write(output, z.next_out - output); + } while (z.avail_out == 0); +} + void GzipOutputStream::Finish() { diff --git a/src/lib/zlib/GzipOutputStream.hxx b/src/lib/zlib/GzipOutputStream.hxx index 88b465c95..3c91daf0e 100644 --- a/src/lib/zlib/GzipOutputStream.hxx +++ b/src/lib/zlib/GzipOutputStream.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2018 Max Kellermann + * Copyright 2014-2021 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -54,6 +54,11 @@ public: explicit GzipOutputStream(OutputStream &_next); ~GzipOutputStream(); + /** + * Throws on error. + */ + void SyncFlush(); + /** * Finish the file and write all data remaining in zlib's * output buffer.