public class ArchiveBuilder
extends java.lang.Object
Supports creating temporary archives that will be deleted on exit, if no name is
specified. The created file must be a Java archive type, with the extension .zip
,
.jar
, .war
or .ear
.
Example:
File zip = ArchiveBuilder.archive("data/archive.zip") .addAt(new File("./pom.xml"), "") .addDirContentsAt(new File("./src"), "src/") .addAt(new File("/tmp/Extra.java"), "src/main/java/") .addDirContentsAt(new File("/tmp/overlay/"), "") .create();
Modifier and Type | Method and Description |
---|---|
ArchiveBuilder |
addAt(java.io.File file,
java.lang.String archiveParentDir)
Adds the given file or directory to the archive, preserving its name but putting under the given directory in the archive (may be
"" or "./" ). |
ArchiveBuilder |
addAtRoot(java.io.File file)
Adds the given file or directory to the root of the archive, preserving its name.
|
ArchiveBuilder |
addDirContentsAt(java.io.File dir,
java.lang.String archiveParentDir)
Add the contents of the directory
dir to the archive. |
ArchiveBuilder |
addDirContentsAtRoot(java.io.File dir)
|
ArchiveBuilder |
addFromLocalBaseDir(java.io.File baseDir,
java.lang.String fileSubPath)
Add a file located at the
fileSubPath , relative to the baseDir on the local system,
as fileSubPath in the archive. |
static ArchiveBuilder |
archive(java.lang.String archive)
Create an
ArchiveBuilder for an archive with the given name. |
java.io.File |
create()
Generates the archive.
|
java.io.File |
create(java.lang.String archiveFile)
Generates the archive, saving it with the given name.
|
ArchiveBuilder |
entries(java.util.Map<java.lang.String,java.io.File> entries)
Add a
Map of entries to the archive. |
ArchiveBuilder |
entry(java.lang.String entryPath,
java.io.File file)
Add the
file to the archive with the path entryPath . |
ArchiveBuilder |
entry(java.lang.String entryPath,
java.lang.String filePath)
Add the
file to the archive with the path entryPath . |
static ArchiveBuilder |
jar()
Create an
ArchiveBuilder for a Jar format archive. |
ArchiveBuilder |
manifest(java.lang.Object key,
java.lang.Object value)
Add a manifest entry with the given
key and value . |
ArchiveBuilder |
named(java.io.File file) |
ArchiveBuilder |
named(java.lang.String name)
Set the location of the generated archive file.
|
void |
stream(java.io.OutputStream output)
Generates the archive and outputs it to the given stream, ignoring any file name.
|
static ArchiveBuilder |
zip()
Create an
ArchiveBuilder for a Zip format archive. |
public static ArchiveBuilder archive(java.lang.String archive)
ArchiveBuilder
for an archive with the given name.public static ArchiveBuilder zip()
ArchiveBuilder
for a Zip
format archive.public static ArchiveBuilder jar()
ArchiveBuilder
for a Jar
format archive.public ArchiveBuilder named(java.lang.String name)
public ArchiveBuilder named(java.io.File file)
named(String)
public ArchiveBuilder manifest(java.lang.Object key, java.lang.Object value)
key
and value
.public ArchiveBuilder addFromLocalBaseDir(java.io.File baseDir, java.lang.String fileSubPath)
fileSubPath
, relative to the baseDir
on the local system,
as fileSubPath
in the archive. For most archives directories are supported.
Uses the fileSubPath
as the name of the file in the archive. Note that the
file is found by concatenating the two path components using Os.mergePaths(String...)
,
thus fileSubPath
should not be absolute or point to a location above the current directory.
For a simpler addition mechanism, use addAt(File, String)
.
For complete control over file locations and names in the archive.
use entry(String, String)
directly or entries(Map)
for complete
entry(String, String)
public ArchiveBuilder addAt(java.io.File file, java.lang.String archiveParentDir)
""
or "./"
).
See also addDirContentsAt(File, String)
and addFromLocalBaseDir(File, String)
.public ArchiveBuilder addAtRoot(java.io.File file)
addDirContentsAtRoot(File)
.
See also addAt(File, String)
.public ArchiveBuilder addDirContentsAt(java.io.File dir, java.lang.String archiveParentDir)
dir
to the archive.
The directory's name is not included; use addAt(File, String)
with ""
as the second argument if you want that behavior.entry(String, File)
public ArchiveBuilder addDirContentsAtRoot(java.io.File dir)
public ArchiveBuilder entry(java.lang.String entryPath, java.lang.String filePath)
file
to the archive with the path entryPath
.entry(String, File)
public ArchiveBuilder entry(java.lang.String entryPath, java.io.File file)
file
to the archive with the path entryPath
.public ArchiveBuilder entries(java.util.Map<java.lang.String,java.io.File> entries)
Map
of entries to the archive.
The keys should be the names of the file entries to be added to the archive and
the value should point to the actual File
to be added.
This allows complete control over the directory structure of the eventual archive, as the entry names do not need to bear any relationship to the name or location of the files on the filesystem.
public void stream(java.io.OutputStream output)
This will add a manifest file if the type is a Jar archive.
public java.io.File create(java.lang.String archiveFile)
public java.io.File create()
If no name has been specified, the archive will be created as a temporary file with a unique name, that is deleted on exit. Otherwise, the given name will be used.