All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.europa.esig.dss.asic.signature.asics.AbstractGetDataToSignASiCS Maven / Gradle / Ivy

package eu.europa.esig.dss.asic.signature.asics;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.DSSException;
import eu.europa.esig.dss.DSSUtils;
import eu.europa.esig.dss.InMemoryDocument;
import eu.europa.esig.dss.utils.Utils;

public abstract class AbstractGetDataToSignASiCS {

	private static final String ZIP_ENTRY_DETACHED_FILE = "detached-file";

	/* In case of multi-files and ASiC-S, we need to create a zip with all files to be signed */
	protected DSSDocument createPackageZip(List documents, Date signingDate) {
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos)) {

			for (DSSDocument document : documents) {
				final String documentName = document.getName();
				final String name = documentName != null ? documentName : ZIP_ENTRY_DETACHED_FILE;
				final ZipEntry entryDocument = new ZipEntry(name);
				entryDocument.setTime(signingDate.getTime());
				entryDocument.setMethod(ZipEntry.STORED);
				byte[] byteArray = DSSUtils.toByteArray(document);
				entryDocument.setSize(byteArray.length);
				entryDocument.setCompressedSize(byteArray.length);
				final CRC32 crc = new CRC32();
				crc.update(byteArray);
				entryDocument.setCrc(crc.getValue());
				zos.putNextEntry(entryDocument);
				Utils.write(byteArray, zos);
			}

			zos.finish();

			return new InMemoryDocument(baos.toByteArray(), "package.zip");
		} catch (IOException e) {
			throw new DSSException("Unable to create package.zip file", e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy