public abstract class ArchiveUtil extends BaseArchiveUtil
Modifier and Type | Class and Description |
---|---|
static class |
ArchiveUtil.ArchivedFileInfo
Archived file information descriptor
|
static interface |
ArchiveUtil.ArchiveFileDescriptor
Descriptor for archive file.
|
static interface |
ArchiveUtil.DoInArchiveHandler<T>
Callback for #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler)
|
static interface |
ArchiveUtil.DoInArchiveHandler2<T>
Callback for #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler)
Similar to DoInArchiveHandler but #found method MUST close all Closeable items in deque(stack)
|
Modifier and Type | Field and Description |
---|---|
static java.util.regex.Pattern |
ARCHIVE_PATH_SEP_PATTERN |
static java.lang.String |
ARCHIVE_PATH_SEPARATOR |
static java.lang.String |
DEFAULT_ZIP_ENCODING |
Constructor and Description |
---|
ArchiveUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
doInArchive(ArchiveUtil.ArchiveFileDescriptor archive,
java.lang.String path,
ArchiveUtil.DoInArchiveHandler<T> callback)
Search for entry with specified path in archive and calls callable.
|
static <T> T |
doInArchive(ArchiveUtil.ArchiveFileDescriptor archive,
java.lang.String path,
ArchiveUtil.DoInArchiveHandler2<T> callable)
Similar to #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler) but callback MUST close Closeable elements (third argument)
|
static java.io.InputStream |
extractEntry(java.util.zip.ZipInputStream input,
java.lang.String path)
Extracts and returns the input stream for the file specified inside a zip stream.
|
static java.util.Collection<ArchiveUtil.ArchivedFileInfo> |
getArchivedFolderEntries(ArchiveUtil.ArchiveFileDescriptor archive,
java.lang.String innerPathToFolder)
Returns archived files info for entries in specified archive.
|
static org.apache.commons.compress.archivers.ArchiveInputStream |
getArchiveInputStream(ArchiveType archiveType,
java.io.InputStream inputStream)
Returns the
ArchiveInputStream for the specified archiveType . |
static org.apache.commons.compress.archivers.ArchiveInputStream |
getArchiveInputStream(java.lang.String name,
java.io.InputStream inputStream)
Returns the
ArchiveInputStream for the specified name of the resource. |
static ArchiveType |
getArchiveType(java.lang.String name)
Determines the archive type based on a file name.
|
static java.io.File |
getPackedFile(java.io.File file)
Adds ".gz" to the file name
|
static java.lang.String |
getPackedFileName(java.lang.String fileName)
adds ".gz" to the given file name
|
static java.lang.String |
getParentPath(java.lang.String path)
Similar to PathUtil#getParentPath(String) but support "!/" path separator and only forward slashes allowed.
|
static boolean |
isNameAllowed(java.lang.String entryName) |
static boolean |
isPackedFile(java.io.File file) |
static boolean |
isPackedFileName(java.lang.String fileName) |
static boolean |
isZipContainsEntry(java.io.File zip,
java.lang.String relativePath) |
static byte[] |
packBytes(byte[] unpacked) |
static java.io.File |
packFile(java.io.File srcFile)
Creates a packed file "file.ext.gz" from the given file "file.ext".
|
static void |
packFileTo(java.io.File dstFile,
java.io.File srcFile)
Packs the content of the source file to the destination file.
|
static void |
packStream(java.io.OutputStream out,
java.io.InputStream in)
Packs the entire content of the input stream and puts it into the output stream.
|
static boolean |
packToZip(java.io.File root,
java.io.FilenameFilter filter,
java.util.zip.ZipOutputStream output,
boolean closeZip) |
static boolean |
packZip(java.io.File destFile,
java.util.Collection<java.io.File> sourceFiles) |
static boolean |
packZip(java.io.File destFile,
java.io.FilenameFilter filter,
java.util.Collection<java.io.File> sourceFiles)
Packs files and folders to a zip archive.
|
static boolean |
packZip(java.io.File root,
java.io.FilenameFilter filter,
java.util.zip.ZipOutputStream output)
Same as
packZip(java.io.File, java.util.zip.ZipOutputStream) , but allows to specify
the filename filter additionally. |
static boolean |
packZip(java.io.File root,
java.util.zip.ZipOutputStream output)
Packs a file (or a directory) to a zip archive.
|
static boolean |
packZip(java.io.OutputStream destStream,
java.util.Collection<java.io.File> sourceFiles) |
static java.util.List<java.lang.String> |
splitByArchivePathSeparator(java.lang.String path,
boolean withEmptyLast)
Splits path by #ARCHIVE_PATH_SEPARATOR ("!/").
|
static byte[] |
unpackBytes(byte[] packed) |
static void |
unpackStream(java.io.OutputStream out,
java.io.InputStream in)
Unpacks the entire content of the input stream and puts it into the output stream.
|
static boolean |
unpackZip(java.io.File zip,
java.io.File targetDir) |
static boolean |
unpackZip(java.io.File zip,
java.lang.String pathPrefix,
java.io.File targetDir)
Extract files from zip archive
|
static boolean |
unpackZip(java.util.zip.ZipInputStream input,
java.io.File targetDir)
Deprecated.
input stream should be ZipSlickAwareZipInputStream
|
isZipContainsFolder
public static final java.lang.String DEFAULT_ZIP_ENCODING
public static final java.lang.String ARCHIVE_PATH_SEPARATOR
public static final java.util.regex.Pattern ARCHIVE_PATH_SEP_PATTERN
@NotNull public static ArchiveType getArchiveType(@NotNull java.lang.String name)
Currently the following extensions are supported:
.zip
, .nupkg
, .snupkg
, .sit
.jar
, .war
, .ear
, .apk
.tar.gz
, .tgz
, .tar.gzip
.tar
name
- the file or resource name@Nullable public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream(@NotNull ArchiveType archiveType, @NotNull java.io.InputStream inputStream) throws java.io.IOException
ArchiveInputStream
for the specified archiveType
.
archiveType
- the type of archiveinputStream
- the input that should be readArchiveInputStream
, or null
if nothing matches.java.io.IOException
- if I/O error occurs@Nullable public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream(@NotNull java.lang.String name, @NotNull java.io.InputStream inputStream) throws java.io.IOException
ArchiveInputStream
for the specified name
of the resource.
name
- the name of the resourceinputStream
- the input that should be readArchiveInputStream
, or null
if nothing matches.java.io.IOException
- if I/O error occurspublic static boolean unpackZip(@NotNull java.io.File zip, @NotNull java.lang.String pathPrefix, @NotNull java.io.File targetDir)
zip
- .zip file to extractpathPrefix
- path prefix to extract from zip. Use "" to extract all filestargetDir
- target folder to extractpublic static boolean unpackZip(@NotNull java.io.File zip, @NotNull java.io.File targetDir)
@Deprecated public static boolean unpackZip(@NotNull java.util.zip.ZipInputStream input, @NotNull java.io.File targetDir)
input
and stores them in the targetDir
.
The input is closed in the end.
input
- zip input streamtargetDir
- the target directorypublic static boolean packZip(@NotNull java.io.File destFile, @NotNull java.util.Collection<java.io.File> sourceFiles) throws java.io.FileNotFoundException
java.io.FileNotFoundException
public static boolean packZip(@NotNull java.io.File destFile, @Nullable java.io.FilenameFilter filter, @NotNull java.util.Collection<java.io.File> sourceFiles) throws java.io.FileNotFoundException
destFile
- destination zip filefilter
- file name filtersourceFiles
- files or folders to zipjava.io.FileNotFoundException
- if destFile exists but is a directory rather than a regular file,
does not exist but cannot be created, or cannot be opened for any other reasonpublic static boolean packZip(@NotNull java.io.OutputStream destStream, @NotNull java.util.Collection<java.io.File> sourceFiles)
public static boolean packZip(@NotNull java.io.File root, @NotNull java.util.zip.ZipOutputStream output)
Files and directories have relative names, for instance, for the following tree:
foo/bar/File1 /File2 /baz/File3the call
packZip(barDirectory, output)
creates a zip with "/File1", "/File2" and "/baz/File3" files,
and calls packZip(bazDirectory, output)
and packZip(file3, output)
both create a zip
with a single file "/File3".
Output stream is closed in the end.
root
- root file or directoryoutput
- the zip stream to write topublic static boolean packZip(@NotNull java.io.File root, @Nullable java.io.FilenameFilter filter, @NotNull java.util.zip.ZipOutputStream output)
packZip(java.io.File, java.util.zip.ZipOutputStream)
, but allows to specify
the filename filter additionally.root
- root file or directoryfilter
- file name filteroutput
- the zip stream to write topublic static boolean packToZip(@NotNull java.io.File root, @Nullable java.io.FilenameFilter filter, @NotNull java.util.zip.ZipOutputStream output, boolean closeZip)
public static void packStream(@NotNull java.io.OutputStream out, @NotNull java.io.InputStream in) throws java.io.IOException
out
- output streamin
- input streamjava.io.IOException
public static void unpackStream(@NotNull java.io.OutputStream out, @NotNull java.io.InputStream in) throws java.io.IOException
out
- output streamin
- input streamjava.io.IOException
@Nullable public static byte[] packBytes(@Nullable byte[] unpacked)
@Nullable public static byte[] unpackBytes(@Nullable byte[] packed)
@Nullable public static java.io.InputStream extractEntry(@NotNull java.util.zip.ZipInputStream input, @NotNull java.lang.String path) throws java.io.IOException
null
is returned.
Note: method does not close the input
.
input
- the zip input streampath
- relative path of the file to extractjava.io.IOException
- if I/O error occurs@NotNull public static java.io.File getPackedFile(@NotNull java.io.File file)
file
- file@NotNull public static java.lang.String getPackedFileName(@NotNull java.lang.String fileName)
fileName
- file namepublic static boolean isPackedFile(@NotNull java.io.File file)
public static boolean isPackedFileName(@NotNull java.lang.String fileName)
@NotNull public static java.io.File packFile(@NotNull java.io.File srcFile) throws java.io.IOException
srcFile
- the given filejava.io.IOException
- if a problem occurs during file operationspublic static void packFileTo(@NotNull java.io.File dstFile, @NotNull java.io.File srcFile) throws java.io.IOException
dstFile
- destination filesrcFile
- source filejava.io.IOException
- if a problem occurs during file operationspublic static boolean isZipContainsEntry(java.io.File zip, java.lang.String relativePath) throws java.io.IOException
java.io.IOException
@NotNull public static java.util.Collection<ArchiveUtil.ArchivedFileInfo> getArchivedFolderEntries(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull java.lang.String innerPathToFolder) throws java.io.IOException
archive
- - a particular archive tp processinnerPathToFolder
- may contains "!/", e.g. "/", "/folder1/", "/folder/archive.zip!/", "/foo/archive.zip!/baz"java.io.IOException
- if IOException occurs@NotNull public static java.util.List<java.lang.String> splitByArchivePathSeparator(@NotNull java.lang.String path, boolean withEmptyLast)
path
- path to processwithEmptyLast
- is should include empty string into result if path ends with "!/"public static <T> T doInArchive(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull java.lang.String path, @NotNull ArchiveUtil.DoInArchiveHandler<T> callback) throws java.io.IOException
T
- callback return typearchive
- - a particular archive to search inpath
- - path to search for, e.g. "a.txt", "foo/baz.txt", "foo/bar.zip!/baz.txt"callback
- function called on entry foundjava.io.FileNotFoundException
- if entry not foundjava.io.IOException
- if archive processing error or callback returns error or failed to get input stream from archivepublic static <T> T doInArchive(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull java.lang.String path, @NotNull ArchiveUtil.DoInArchiveHandler2<T> callable) throws java.io.IOException
java.io.IOException
@NotNull public static java.lang.String getParentPath(@NotNull java.lang.String path)
path
- path to process.public static boolean isNameAllowed(@Nullable java.lang.String entryName)