00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pandabase.h"
00020 #include "zStream.h"
00021 #include "filename.h"
00022
00023 #include "zlib.h"
00024
00025 void
00026 stream_decompress(istream &source) {
00027 IDecompressStream zstream(&source, false);
00028
00029 int ch = zstream.get();
00030 while (!zstream.eof() && !zstream.fail()) {
00031 cout.put(ch);
00032 ch = zstream.get();
00033 }
00034 }
00035
00036 void
00037 stream_compress(istream &source) {
00038 OCompressStream zstream(&cout, false);
00039
00040 int ch = source.get();
00041 while (!source.eof() && !source.fail()) {
00042 zstream.put(ch);
00043 ch = source.get();
00044 }
00045 }
00046
00047 void
00048 zlib_decompress(istream &source) {
00049
00050 string data;
00051
00052 int ch = source.get();
00053 while (!source.eof() && !source.fail()) {
00054 data += (char)ch;
00055 ch = source.get();
00056 }
00057
00058
00059 size_t source_len = data.length();
00060
00061 size_t dest_len = source_len * 4;
00062 char *dest = new char[dest_len];
00063
00064 uLongf actual_dest_len = dest_len;
00065 int result = uncompress((Bytef *)dest, &actual_dest_len,
00066 (const Bytef *)data.data(), source_len);
00067 if (result != Z_OK) {
00068 cerr << "compress result == " << result << "\n";
00069 }
00070
00071 while (result == Z_BUF_ERROR) {
00072 dest_len *= 2;
00073 cerr << "Increasing buffer size to " << dest_len << "\n";
00074 delete[] dest;
00075 dest = new char[dest_len];
00076
00077 actual_dest_len = dest_len;
00078 result = uncompress((Bytef *)dest, &actual_dest_len,
00079 (const Bytef *)data.data(), source_len);
00080 if (result != Z_OK) {
00081 cerr << "compress result == " << result << "\n";
00082 }
00083 }
00084
00085 cout.write(dest, actual_dest_len);
00086 }
00087
00088 void
00089 zlib_compress(istream &source) {
00090
00091 string data;
00092
00093 int ch = source.get();
00094 while (!source.eof() && !source.fail()) {
00095 data += (char)ch;
00096 ch = source.get();
00097 }
00098
00099
00100 size_t source_len = data.length();
00101 size_t dest_len = (size_t)(source_len * 1.1) + 12;
00102 char *dest = new char[dest_len];
00103
00104 uLongf actual_dest_len = dest_len;
00105 int result = compress((Bytef *)dest, &actual_dest_len,
00106 (const Bytef *)data.data(), source_len);
00107
00108 if (result != Z_OK) {
00109 cerr << "compress result == " << result << "\n";
00110 }
00111
00112 cout.write(dest, actual_dest_len);
00113 }
00114
00115 int
00116 main(int argc, char *argv[]) {
00117 bool zlib_direct = false;
00118
00119 if (argc >= 2 && strcmp(argv[1], "-z") == 0) {
00120 zlib_direct = true;
00121 argc--;
00122 argv++;
00123 }
00124
00125 if (argc != 2) {
00126 cerr << "test_zstream [-z] file\n"
00127 << "compresses file to standard output, or decompresses it if the\n"
00128 << "filename ends in .pz.\n\n"
00129 << "With -z, calls zlib directly instead of using the zstream interface.\n";
00130 return (1);
00131 }
00132
00133 Filename source_filename = argv[1];
00134 source_filename.set_binary();
00135
00136 ifstream source;
00137
00138 if (!source_filename.open_read(source)) {
00139 cerr << "Unable to open source " << source_filename << ".\n";
00140 return (1);
00141 }
00142
00143 if (zlib_direct) {
00144 if (source_filename.get_extension() == "pz") {
00145 zlib_decompress(source);
00146 } else {
00147 zlib_compress(source);
00148 }
00149 } else {
00150 if (source_filename.get_extension() == "pz") {
00151 stream_decompress(source);
00152 } else {
00153 stream_compress(source);
00154 }
00155 }
00156
00157 return (0);
00158 }