Skip to content

Commit 9320abe

Browse files
committed
feat: gzip cache output
1 parent 673cd1b commit 9320abe

File tree

2 files changed

+15
-8
lines changed
  • open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd
  • vulnz/src/main/java/io/github/jeremylong/vulnz/cli/commands

2 files changed

+15
-8
lines changed

open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd/NvdCveClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ public Collection<DefCveItem> next() {
311311
this.indexesToRetrieve.remove(call.getStartIndex());
312312
} catch (JsonProcessingException e) {
313313
return next();
314-
// throw new NvdApiException("Failed to parse JSON starting with: \"" + json.substring(0, 100) + "\"",
315-
// e);
314+
// throw new NvdApiException("Failed to parse JSON starting with: \"" + json.substring(0, 100) +
315+
// "\"",
316+
// e);
316317
}
317318
this.totalAvailable = current.getTotalResults();
318319
lastUpdated = findLastUpdated(lastUpdated, current.getVulnerabilities());

vulnz/src/main/java/io/github/jeremylong/vulnz/cli/commands/CveCommand.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import java.io.File;
4141
import java.io.FileInputStream;
42+
import java.io.FileOutputStream;
4243
import java.io.IOException;
4344
import java.io.InputStream;
4445
import java.time.Year;
@@ -50,6 +51,8 @@
5051
import java.util.List;
5152
import java.util.Map;
5253
import java.util.Optional;
54+
import java.util.zip.GZIPInputStream;
55+
import java.util.zip.GZIPOutputStream;
5356

5457
import static com.diogonunes.jcolor.Ansi.colorize;
5558

@@ -233,12 +236,14 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
233236
final String prefix = properties.get("prefix", "nvdcve-");
234237
// load existing cached files
235238
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");
237240
cves.put(Integer.toString(year), new HashMap<>());
238241
if (file.isFile()) {
239242
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);
242247
} catch (IOException exception) {
243248
throw new CacheException("Unable to read cached data: " + file, exception);
244249
}
@@ -266,7 +271,7 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
266271
final String version = "2.0";
267272

268273
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");
270275
List<DefCveItem> vulnerabilities = new ArrayList(entry.getValue().values());
271276
vulnerabilities.sort((v1, v2) -> {
272277
return v1.getCve().getId().compareTo(v2.getCve().getId());
@@ -284,8 +289,9 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
284289
properties.set("lastModifiedDate." + entry.getKey(), timestamp);
285290
CveApiJson20 data = new CveApiJson20(vulnerabilities.size(), 0, vulnerabilities.size(), format, version,
286291
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);
289295
} catch (IOException ex) {
290296
throw new CacheException("Unable to write cached data: " + file, ex);
291297
}

0 commit comments

Comments
 (0)