File: zipcppstdbuf.h

package info (click to toggle)
dosbox-x 2026.01.02%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,220 kB
  • sloc: cpp: 341,269; ansic: 165,494; sh: 1,463; makefile: 967; perl: 385; python: 106; asm: 57
file content (79 lines) | stat: -rw-r--r-- 1,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#ifndef ZIPSTREAMBUF
#define ZIPSTREAMBUF

#include "zip.h"
#include "unzip.h"
#include "ioapi.h"

#include <assert.h>

/* std::streambuf for writing to ZIP archive directly.
 * ZIP archive writer can only write one file at a time, do not
 * use multiple instances of this C++ class at a time! No seeking,
 * only sequential output! */
class zip_ostreambuf : public std::streambuf {
public:
	using Base = std::streambuf;
public:
	zip_ostreambuf(zipFile &n_zf) : basic_streambuf(), zf(n_zf) { }
	virtual ~zip_ostreambuf() { close(); }
public:
	virtual std::streamsize xsputn(const Base::char_type *s, std::streamsize count) override {
		assert(zf != NULL);

		const int err = zipWriteInFileInZip(zf, (void*)s, count);
		if (err != ZIP_OK) {
			zf_err = err;
			return 0;
		}

		return count;
	}
public:
	int close(void) {
		int err;

		if ((err=zipCloseFileInZip(zf)) != ZIP_OK) return err;
		if (zf_err != ZIP_OK) return zf_err;
		return ZIP_OK;
	}
private:
	zipFile zf = NULL;
	int zf_err = ZIP_OK;
};

class zip_istreambuf : public std::streambuf {
public:
	using Base = std::streambuf;
public:
	zip_istreambuf(unzFile &n_zf) : basic_streambuf(), zf(n_zf) { }
	virtual ~zip_istreambuf() { close(); }
public:
	virtual std::streamsize xsgetn(Base::char_type *s, std::streamsize count) override {
		assert(zf != NULL);

		const int err = unzReadCurrentFile(zf, (void*)s, count);
		if (err < 0) {
			zf_err = err;
			return 0;
		}

		return std::streamsize(err);
	}
public:
	int close(void) {
		int err;

		if ((err=unzCloseCurrentFile(zf)) != UNZ_OK) return err;
		if (zf_err != UNZ_OK) return zf_err;
		return UNZ_OK;
	}
private:
	unzFile zf = NULL;
	int zf_err = UNZ_OK;
};


#endif