use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.
the class CsarExporter method addDummyRepositoryFileReferenceForGeneratedXSD.
/**
* Adds a dummy file to the archive
*
* @param zos Output stream of the archive
* @param transformer Given transformer to transform the {@link DummyRepositoryFileReferenceForGeneratedXSD} to a
* {@link ArchiveOutputStream}
* @param ref The dummy document that should be exported as an archive
* @param archivePath The output path of the archive
*/
private void addDummyRepositoryFileReferenceForGeneratedXSD(ArchiveOutputStream zos, Transformer transformer, DummyRepositoryFileReferenceForGeneratedXSD ref, String archivePath) throws IOException {
ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
zos.putArchiveEntry(archiveEntry);
CsarExporter.LOGGER.trace("Special treatment for generated XSDs");
Document document = ref.getDocument();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(zos);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
CsarExporter.LOGGER.debug("Could not serialize generated xsd", e);
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.
the class CsarExporter method addNamespacePrefixes.
/**
* Writes the configured mapping namespaceprefix -> namespace to the archive
* <p>
* This is kind of a quick hack. TODO: during the import, the prefixes should be extracted using JAXB and stored in
* the NamespacesResource
*/
private void addNamespacePrefixes(ArchiveOutputStream zos, IRepository repository) throws IOException {
Configuration configuration = repository.getConfiguration(new NamespacesId());
if (configuration instanceof PropertiesConfiguration) {
// Quick hack: direct serialization only works for PropertiesConfiguration
PropertiesConfiguration pconf = (PropertiesConfiguration) configuration;
ArchiveEntry archiveEntry = new ZipArchiveEntry(CsarExporter.PATH_TO_NAMESPACES_PROPERTIES);
zos.putArchiveEntry(archiveEntry);
try {
pconf.save(zos);
} catch (ConfigurationException e) {
CsarExporter.LOGGER.debug(e.getMessage(), e);
zos.write("#Could not export properties".getBytes());
zos.write(("#" + e.getMessage()).getBytes());
}
zos.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.
the class CsarExporter method addWorkingTreeToArchive.
/**
* Adds a working tree to an archive
*
* @param file The current directory to add
* @param zos Output stream of the archive
* @param template Template of the artifact
* @param rootDir The root of the working tree
* @param archivePath The path inside the archive to the working tree
*/
private void addWorkingTreeToArchive(File file, ArchiveOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
if (file.isDirectory()) {
if (file.getName().equals(".git")) {
return;
}
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
addWorkingTreeToArchive(f, zos, template, rootDir, archivePath);
}
}
} else {
boolean foundInclude = false;
boolean included = false;
boolean excluded = false;
for (TArtifactReference artifactReference : template.getArtifactReferences().getArtifactReference()) {
for (Object includeOrExclude : artifactReference.getIncludeOrExclude()) {
if (includeOrExclude instanceof TArtifactReference.Include) {
foundInclude = true;
TArtifactReference.Include include = (TArtifactReference.Include) includeOrExclude;
String reference = artifactReference.getReference();
if (reference.endsWith("/")) {
reference += include.getPattern();
} else {
reference += "/" + include.getPattern();
}
reference = reference.substring(1);
included |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
} else if (includeOrExclude instanceof TArtifactReference.Exclude) {
TArtifactReference.Exclude exclude = (TArtifactReference.Exclude) includeOrExclude;
String reference = artifactReference.getReference();
if (reference.endsWith("/")) {
reference += exclude.getPattern();
} else {
reference += "/" + exclude.getPattern();
}
reference = reference.substring(1);
excluded |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
}
}
}
if ((!foundInclude || included) && !excluded) {
try (InputStream is = new FileInputStream(file)) {
ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath + rootDir.relativize(Paths.get(file.getAbsolutePath())));
zos.putArchiveEntry(archiveEntry);
IOUtils.copy(is, zos);
zos.closeArchiveEntry();
} catch (Exception e) {
CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
}
}
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.
the class CsarExporter method addArtifactTemplateToZipFile.
/**
* Special handling for artifact template directories source and files
*
* @param zos Output stream for the archive that should contain the file
* @param ref Reference to the file that should be added to the archive
* @param archivePath Path to the file inside the archive
* @throws IOException thrown when the temporary directory can not be created
*/
private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref, String archivePath) throws IOException {
GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());
if (gitInfo == null) {
try (InputStream is = repository.newInputStream(ref)) {
if (is != null) {
ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
zos.putArchiveEntry(archiveEntry);
IOUtils.copy(is, zos);
zos.closeArchiveEntry();
}
} catch (Exception e) {
CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
}
return;
}
// TODO: This is not quite correct. The files should reside checked out at "source/"
Path tempDir = Files.createTempDirectory(WINERY_TEMP_DIR_PREFIX);
try {
Git git = Git.cloneRepository().setURI(gitInfo.URL).setDirectory(tempDir.toFile()).call();
git.checkout().setName(gitInfo.BRANCH).call();
String path = "artifacttemplates/" + Util.URLencode(((ArtifactTemplateId) ref.getParent().getParent()).getQName().getNamespaceURI()) + "/" + ((ArtifactTemplateId) ref.getParent().getParent()).getQName().getLocalPart() + "/files/";
TArtifactTemplate template = BackendUtils.getTArtifactTemplate((DirectoryId) ref.getParent());
addWorkingTreeToArchive(zos, template, tempDir, path);
} catch (GitAPIException e) {
CsarExporter.LOGGER.error(String.format("Error while cloning repo: %s / %s", gitInfo.URL, gitInfo.BRANCH), e);
} finally {
deleteDirectory(tempDir);
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project cuba by cuba-platform.
the class EntityImportExport method exportEntitiesToZIP.
@Override
public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) {
String json = entitySerialization.toJson(entities, null, EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes);
try {
zipOutputStream.putArchiveEntry(singleDesignEntry);
zipOutputStream.write(jsonBytes);
zipOutputStream.closeArchiveEntry();
} catch (Exception e) {
throw new RuntimeException("Error on creating zip archive during entities export", e);
} finally {
IOUtils.closeQuietly(zipOutputStream);
}
return byteArrayOutputStream.toByteArray();
}
Aggregations