1- #include < iostream>
2- #include < vector>
3- #include " zlib.h"
4- int UnsafeRead () {
1+
2+ int UnsafeInflate (int argc, char *argv[]) {
3+ // original string len = 36
4+ char a[50 ] = " Hello Hello Hello Hello Hello Hello!" ;
5+ // placeholder for the compressed (deflated) version of "a"
6+ char b[50 ];
7+ // placeholder for the Uncompressed (inflated) version of "b"
8+ char c[50 ];
9+ printf (" Uncompressed size is: %lu\n " , strlen (a));
10+ printf (" Uncompressed string is: %s\n " , a);
11+ printf (" \n ----------\n\n " );
12+
13+ // STEP 1.
14+ // zlib struct
15+ z_stream defstream;
16+ defstream.zalloc = Z_NULL;
17+ defstream.zfree = Z_NULL;
18+ defstream.opaque = Z_NULL;
19+ // setup "a" as the input and "b" as the compressed output
20+ defstream.avail_in = (uInt) strlen (a) + 1 ; // size of input, string + terminator
21+ defstream.next_in = (Bytef *) a; // input char array
22+ defstream.avail_out = (uInt) sizeof (b); // size of output
23+ defstream.next_out = (Bytef *) b; // output char array
24+
25+ // the actual compression work.
26+ deflateInit (&defstream, Z_BEST_COMPRESSION);
27+ deflate (&defstream, Z_FINISH);
28+ deflateEnd (&defstream);
29+
30+ // This is one way of getting the size of the output
31+ printf (" Compressed size is: %lu\n " , strlen (b));
32+ printf (" Compressed string is: %s\n " , b);
33+ printf (" \n ----------\n\n " );
34+ // STEP 2.
35+ // inflate b into c
36+ // zlib struct
37+ z_stream infstream;
38+ infstream.zalloc = Z_NULL;
39+ infstream.zfree = Z_NULL;
40+ infstream.opaque = Z_NULL;
41+ // setup "b" as the input and "c" as the compressed output
42+ // TOTHINK: Here we can add additional step from Right operand to z_stream variable access
43+ infstream.avail_in = (uInt) ((char *) defstream.next_out - b); // size of input
44+ infstream.next_in = (Bytef *) b; // input char array
45+ infstream.avail_out = (uInt) sizeof (c); // size of output
46+ infstream.next_out = (Bytef *) c; // output char array
47+
48+ // uLong total_out; /* total number of bytes output so far */
49+ // the actual DE-compression work.
50+ inflateInit (&infstream);
51+ std::cout << infstream.total_out << std::endl;
52+ inflate (&infstream, Z_NO_FLUSH);
53+ std::cout << infstream.total_out << std::endl;
54+ inflateEnd (&infstream);
55+
56+ printf (" Uncompressed size is: %lu\n " , strlen (c));
57+ printf (" Uncompressed string is: %s\n " , c);
58+ return 0 ;
59+ }
60+
61+ int UnsafeGzread () {
562 std::cout << " enter compressed file name!\n " << std::endl;
663 char fileName[100 ];
764 std::cin >> fileName;
@@ -21,10 +78,47 @@ int UnsafeRead() {
2178 break ;
2279 }
2380 }
81+ for (auto &&i: unzippedData)
82+ std::cout << i;
83+ gzclose (inFileZ);
84+ return 0 ;
85+ }
2486
25- for ( auto &&i: unzippedData)
26- std::cout << i;
87+ int UnsafeGzfread () {
88+ std::cout << " enter compressed file name!\n " << std::endl;
89+ char fileName[100 ];
90+ std::cin >> fileName;
91+ gzFile inFileZ = gzopen (fileName, " rb" );
92+ if (inFileZ == nullptr ) {
93+ printf (" Error: Failed to gzopen %s\n " , fileName);
94+ exit (0 );
95+ }
96+ while (true ) {
97+ char buffer[1000 ];
98+ if (!gzfread (buffer, 999 , 1 , inFileZ)) {
99+ break ;
100+ }
101+ }
27102 gzclose (inFileZ);
103+ return 0 ;
104+ }
28105
106+ int UnsafeGzgets () {
107+ std::cout << " enter compressed file name!\n " << std::endl;
108+ char fileName[100 ];
109+ std::cin >> fileName;
110+ gzFile inFileZ = gzopen (fileName, " rb" );
111+ if (inFileZ == nullptr ) {
112+ printf (" Error: Failed to gzopen %s\n " , fileName);
113+ exit (0 );
114+ }
115+ char *buffer = new char [4000000000 ];
116+ char *result = gzgets (inFileZ, buffer, 1000000000 );
117+ while (true ) {
118+ result = gzgets (inFileZ, buffer, 1000000000 );
119+ if (result == nullptr ) {
120+ break ;
121+ }
122+ }
29123 return 0 ;
30- }
124+ }
0 commit comments