Try to read and write GZ compressed files using C + + Gzlib and Java Gzipoutputstream

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.