00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "filename.h"
00020 #include "zStream.h"
00021 #include "notify.h"
00022
00023 int
00024 main(int argc, char *argv[]) {
00025 if (argc < 2) {
00026 cerr << "Usage: pcompress <file> [<dest_file>]" << endl;
00027 return 1;
00028 }
00029
00030 bool implicit_dest_file;
00031 Filename source_file = Filename::from_os_specific(argv[1]);
00032 Filename dest_file;
00033 if (argc < 3) {
00034 dest_file = source_file.get_fullpath() + ".pz";
00035 implicit_dest_file = true;
00036 } else {
00037 dest_file = Filename::from_os_specific(argv[2]);
00038 implicit_dest_file = false;
00039 }
00040
00041
00042 ifstream read_stream;
00043 source_file.set_binary();
00044 if (!source_file.open_read(read_stream)) {
00045 cerr << "failed to open: " << source_file << endl;
00046 return 1;
00047 }
00048
00049
00050 read_stream.seekg(0, ios::end);
00051 int source_file_length = read_stream.tellg();
00052 read_stream.seekg(0, ios::beg);
00053
00054 if (source_file_length == 0) {
00055 cerr << "zero length file: " << source_file << endl;
00056 return 1;
00057 }
00058
00059
00060 ofstream write_stream;
00061 dest_file.set_binary();
00062 if (!dest_file.open_write(write_stream, true)) {
00063 cerr << "failed to open: " << dest_file << endl;
00064 return 1;
00065 }
00066
00067 {
00068 OCompressStream compress(&write_stream, false);
00069
00070 int ch = read_stream.get();
00071 while (!read_stream.eof() && !read_stream.fail()) {
00072 compress.put(ch);
00073 ch = read_stream.get();
00074 }
00075 }
00076
00077 read_stream.close();
00078 write_stream.close();
00079
00080 if (implicit_dest_file) {
00081 source_file.unlink();
00082 }
00083
00084 return 0;
00085 }