Issue background:
I need to generate a type of double matrix of about 1.5T, the hard disk can not stand, io time is not consumed, so try to compress before the output. Matrix generation using Java, calculations on matrices using CPP
Then try to use Java to write GZ compressed files and use C + + read compressed files
For a simple test code:
Write:
public boolean generategzipfile (String ofname) throws IOException { FileOutputStream fout = new FileOutputStream ( Ofname); Gzipoutputstream Gos = new Gzipoutputstream (fout); for (int i = 0; i < numbers.size (); i++) { String tstr = string.valueof (Numbers.get (i)); Tstr + = "\ n"; Gos.write (Tstr.getbytes ()); } Gos.finish (); Gos.flush (); Gos.close (); return true; }
Read:
#include <iostream> #include <stdlib.h> #include <string> #include <zlib.h>using namespace std; int main (int argc, char **argv) { if (argc! = 2) { Cerr << "Example Command:exe file.tar.gz" << endl;
return 0; } Gzfile GZFP = Gzopen (argv[1], "RB"); if (!GZFP) return 0; Char buf[32]; while (Gzgets (GZFP, buf)) { cout << "str:" << buf << Endl; cout << "num:" << atof (BUF) << Endl; } return 0;}
Issues to be aware of:
Gzipoutputstream has only one write method, only accepts byte[], so you need to convert double to byte[], there are two paths:
1. Double direct conversion to byte[]
byte dnum[] = new BYTE[8]; Bytebuffer.wrap (Dnum). Putdouble (Numbers.get (i)); Gos.write (Dnum);
This method writes the file, cannot use the Gunzip direct decompression, the decompression will be garbled, because the file storage double value is according to each number char as a byte to store, can only write the decompression program itself, will byte[] turn into double
2. Double-turn a string and turn byte[]
That's the way I end up using it.
Finally, do some tests, gzip compression effect really praise
-rw-r--r--1 Users 193843 APR t10e4
-rw-r--r--1 Users 5408 APR t10e4.gz
-rw-r--r--1 Users 1977688 APR t10e5
-rw-r--r--1 Users 80940 APR t10e5.gz
Try to read and write GZ compressed files using C + + Gzlib and Java Gzipoutputstream