Common base implementations for the ShrinkWrap project enabling declarative assembly of Java archives (JAR, WAR, EAR, RAR) in code
—
The import and export system provides comprehensive support for converting archives to and from various formats including ZIP, TAR (with compression options), and exploded directory structures.
Base implementation for all stream-based exporters.
public abstract class AbstractStreamExporterImpl implements StreamExporterKey Methods:
public InputStream exportAsInputStream()
public void exportTo(OutputStream outputStream)
public void exportTo(File file)Delegation pattern base for custom exporters.
public abstract class AbstractExporterDelegate<T> implements Exporter<T>Standard ZIP format exporter with compression.
public class ZipExporterImpl extends AbstractStreamExporterImpl implements ZipExporterKey Features:
Usage:
Archive<?> archive = // ... create archive
InputStream zipStream = archive.as(ZipExporter.class).exportAsInputStream();
// Export to file
File zipFile = new File("archive.zip");
archive.as(ZipExporter.class).exportTo(zipFile);Uncompressed ZIP format exporter for faster processing.
public class ZipStoredExporterImpl extends AbstractStreamExporterImpl implements ZipExporterKey Features:
Usage:
// Export without compression for speed
archive.as(ZipStoredExporter.class).exportTo(outputStream);Standard TAR format exporter.
public class TarExporterImpl extends AbstractStreamExporterImpl implements TarExporterKey Features:
GZIP-compressed TAR format exporter.
public class TarGzExporterImpl extends AbstractStreamExporterImpl implements TarGzExporterKey Features:
BZIP2-compressed TAR format exporter.
public class TarBz2ExporterImpl extends AbstractStreamExporterImpl implements TarBz2ExporterKey Features:
Usage Examples:
// Standard TAR
archive.as(TarExporter.class).exportTo("archive.tar");
// GZIP compressed
archive.as(TarGzExporter.class).exportTo("archive.tar.gz");
// BZIP2 compressed
archive.as(TarBz2Exporter.class).exportTo("archive.tar.bz2");Exports archives to exploded directory structure.
public class ExplodedExporterImpl implements ExplodedExporterKey Methods:
public File exportExploded(File directory)
public void exportExploded(File directory, Filter<ArchivePath> filter)Usage:
// Export to directory
File explodedDir = new File("exploded-archive");
archive.as(ExplodedExporter.class).exportExploded(explodedDir);
// Export with filtering
archive.as(ExplodedExporter.class)
.exportExploded(explodedDir, Filters.exclude(".*\\.tmp"));Imports archives from ZIP format streams and files.
public class ZipImporterImpl implements ZipImporterKey Methods:
public Archive<?> importFrom(File file)
public Archive<?> importFrom(InputStream inputStream)
public Archive<?> importFrom(InputStream inputStream, String archiveName)Usage:
// Import from file
Archive<?> archive = ShrinkWrap.create(ZipImporter.class)
.importFrom(new File("source.zip"))
.as(GenericArchive.class);
// Import from stream
InputStream zipStream = // ... get stream
Archive<?> archive = ShrinkWrap.create(ZipImporter.class)
.importFrom(zipStream, "imported.zip")
.as(JavaArchive.class);Standard TAR format importer.
public class TarImporterImpl implements TarImporterGZIP-compressed TAR format importer.
public class TarGzImporterImpl implements TarGzImporterBZIP2-compressed TAR format importer.
public class TarBz2ImporterImpl implements TarBz2ImporterUsage Examples:
// Standard TAR
Archive<?> archive = ShrinkWrap.create(TarImporter.class)
.importFrom(new File("archive.tar"))
.as(GenericArchive.class);
// GZIP compressed
Archive<?> archive = ShrinkWrap.create(TarGzImporter.class)
.importFrom(new File("archive.tar.gz"))
.as(JavaArchive.class);
// BZIP2 compressed
Archive<?> archive = ShrinkWrap.create(TarBz2Importer.class)
.importFrom(new File("archive.tar.bz2"))
.as(WebArchive.class);Imports archives from exploded directory structures.
public class ExplodedImporterImpl implements ExplodedImporterKey Methods:
public Archive<?> importDirectory(File directory)
public Archive<?> importDirectory(File directory, Filter<ArchivePath> filter)Usage:
// Import entire directory
Archive<?> archive = ShrinkWrap.create(ExplodedImporter.class)
.importDirectory(new File("exploded-webapp"))
.as(WebArchive.class);
// Import with filtering
Archive<?> archive = ShrinkWrap.create(ExplodedImporter.class)
.importDirectory(sourceDir, Filters.exclude(".*\\.bak"))
.as(JavaArchive.class);All importers and exporters support streaming for memory efficiency:
// Stream-based export
try (OutputStream out = new FileOutputStream("archive.zip")) {
archive.as(ZipExporter.class).exportTo(out);
}
// Stream-based import
try (InputStream in = new FileInputStream("source.zip")) {
Archive<?> archive = ShrinkWrap.create(ZipImporter.class)
.importFrom(in, "imported")
.as(GenericArchive.class);
}Import and export operations support content filtering:
// Export with filtering
Filter<ArchivePath> filter = Filters.exclude(".*\\.tmp", ".*\\.log");
archive.as(ExplodedExporter.class).exportExploded(targetDir, filter);
// Import with filtering
Archive<?> filtered = ShrinkWrap.create(ExplodedImporter.class)
.importDirectory(sourceDir, Filters.include(".*\\.class", ".*\\.xml"))
.as(JavaArchive.class);Automatic format detection based on file extensions and content:
// Automatic format detection
File unknownFile = new File("archive.unknown");
Archive<?> archive = ArchiveFactory.importFrom(unknownFile);TAR exporters support compression level configuration:
// Configure compression level
TarGzExporter exporter = archive.as(TarGzExporter.class);
exporter.setCompressionLevel(9); // Maximum compression
exporter.exportTo("highly-compressed.tar.gz");Import/export operations preserve important metadata:
Common error scenarios and handling:
try {
archive.as(ZipExporter.class).exportTo(file);
} catch (ArchiveExportException e) {
// Handle export errors
logger.error("Export failed: " + e.getMessage(), e);
} catch (IOException e) {
// Handle I/O errors
logger.error("I/O error during export: " + e.getMessage(), e);
}Install with Tessl CLI
npx tessl i tessl/maven-org-jboss-shrinkwrap--shrinkwrap-impl-base