VaultODVExporter.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:BachelorPraktikum 作者:
/**
 * Writes the data to a CSV file and generates a signature hash.
 * Then it puts both of this into a ZIP archive file.
 *
 * @param csvEntries The {@link ExportEntry} to be exported.
 * @throws IOException Thrown if the SHA-512 hash algorithm is missing.
 */
@Override
protected void writeToFile(final List<ExportEntry> csvEntries) throws IOException {
    // Setup compression stuff
    FileOutputStream fileOutputStream = getFileOutputStream();
    final int zipCompressionLevel = 9;

    ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
    zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
    zipOutputStream.setLevel(zipCompressionLevel);

    // Setup signature
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException exception) {
        LOG.log(Level.SEVERE, "Missing hash algorithm for signature. No file exported!", exception);
        throw new IOException("Missing hash algorithm for signature.");
    }
    DigestOutputStream digestOutputStream = new DigestOutputStream(zipOutputStream, digest);

    // write data
    zipOutputStream.putNextEntry(new ZipEntry(DATA_ZIP_ENTRY));
    CsvWriter cwriter = new CsvWriter(digestOutputStream, VaultCsvEntry.CSV_DELIMITER,
            Charset.forName("UTF-8"));

    cwriter.writeRecord(((CsvEntry) csvEntries.get(0)).getCsvHeaderRecord());
    for (ExportEntry item : csvEntries) {
        cwriter.writeRecord(((CsvEntry) item).toCsvRecord());
    }
    cwriter.flush();

    // add signature file
    zipOutputStream.putNextEntry(new ZipEntry(SIGNATURE_ZIP_ENTRY));
    String sigString = (new HexBinaryAdapter()).marshal(digest.digest());
    zipOutputStream.write(sigString.getBytes("UTF-8"), 0, sigString.getBytes("UTF-8").length);

    // Closing the streams
    cwriter.close();
    digestOutputStream.close();
    zipOutputStream.close();
    fileOutputStream.close();
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号