39
39
40
40
import java .io .File ;
41
41
import java .io .FileInputStream ;
42
+ import java .io .FileOutputStream ;
42
43
import java .io .IOException ;
43
44
import java .io .InputStream ;
44
45
import java .time .Year ;
50
51
import java .util .List ;
51
52
import java .util .Map ;
52
53
import java .util .Optional ;
54
+ import java .util .zip .GZIPInputStream ;
55
+ import java .util .zip .GZIPOutputStream ;
53
56
54
57
import static com .diogonunes .jcolor .Ansi .colorize ;
55
58
@@ -233,12 +236,14 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
233
236
final String prefix = properties .get ("prefix" , "nvdcve-" );
234
237
// load existing cached files
235
238
for (int year = 2002 ; year <= Year .now ().getValue (); year ++) {
236
- File file = new File (properties .getDirectory (), prefix + year + ".json" );
239
+ File file = new File (properties .getDirectory (), prefix + year + ".json.gz " );
237
240
cves .put (Integer .toString (year ), new HashMap <>());
238
241
if (file .isFile ()) {
239
242
CveApiJson20 data ;
240
- try (InputStream in = new FileInputStream (file )) {
241
- data = objectMapper .readValue (in , CveApiJson20 .class );
243
+ try (FileInputStream fileInputStream = new FileInputStream (file );
244
+ GZIPInputStream gzipInputStream = new GZIPInputStream (fileInputStream );
245
+ ) {
246
+ data = objectMapper .readValue (gzipInputStream , CveApiJson20 .class );
242
247
} catch (IOException exception ) {
243
248
throw new CacheException ("Unable to read cached data: " + file , exception );
244
249
}
@@ -266,7 +271,7 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
266
271
final String version = "2.0" ;
267
272
268
273
for (Map .Entry <String , HashMap <String , DefCveItem >> entry : cves .entrySet ()) {
269
- File file = new File (properties .getDirectory (), prefix + entry .getKey () + ".json" );
274
+ File file = new File (properties .getDirectory (), prefix + entry .getKey () + ".json.gz " );
270
275
List <DefCveItem > vulnerabilities = new ArrayList (entry .getValue ().values ());
271
276
vulnerabilities .sort ((v1 , v2 ) -> {
272
277
return v1 .getCve ().getId ().compareTo (v2 .getCve ().getId ());
@@ -284,8 +289,9 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
284
289
properties .set ("lastModifiedDate." + entry .getKey (), timestamp );
285
290
CveApiJson20 data = new CveApiJson20 (vulnerabilities .size (), 0 , vulnerabilities .size (), format , version ,
286
291
timestamp , vulnerabilities );
287
- try {
288
- objectMapper .writeValue (file , data );
292
+ try (FileOutputStream fileOutputStream = new FileOutputStream (file );
293
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream (fileOutputStream );) {
294
+ objectMapper .writeValue (gzipOutputStream , data );
289
295
} catch (IOException ex ) {
290
296
throw new CacheException ("Unable to write cached data: " + file , ex );
291
297
}
0 commit comments