Package jetbrains.buildServer.util
Class ArchiveUtil
- java.lang.Object
-
- jetbrains.buildServer.util.BaseArchiveUtil
-
- jetbrains.buildServer.util.ArchiveUtil
-
public abstract class ArchiveUtil extends BaseArchiveUtil
Provides utils to archive and extract files from zip archive
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ArchiveUtil.ArchivedFileInfo
Archived file information descriptorstatic 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)
-
Field Summary
Fields Modifier and Type Field Description static Pattern
ARCHIVE_PATH_SEP_PATTERN
static String
ARCHIVE_PATH_SEPARATOR
static String
DEFAULT_ZIP_ENCODING
-
Constructor Summary
Constructors Constructor Description ArchiveUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
doInArchive(ArchiveUtil.ArchiveFileDescriptor archive, String path, ArchiveUtil.DoInArchiveHandler<T> callback)
Search for entry with specified path in archive and calls callable.static <T> T
doInArchive(ArchiveUtil.ArchiveFileDescriptor archive, String path, ArchiveUtil.DoInArchiveHandler2<T> callable)
Similar to #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler) but callback MUST close Closeable elements (third argument)static InputStream
extractEntry(ZipInputStream input, String path)
Extracts and returns the input stream for the file specified inside a zip stream.static Collection<ArchiveUtil.ArchivedFileInfo>
getArchivedFolderEntries(ArchiveUtil.ArchiveFileDescriptor archive, String innerPathToFolder)
Returns archived files info for entries in specified archive.static org.apache.commons.compress.archivers.ArchiveInputStream
getArchiveInputStream(String name, InputStream inputStream)
Returns theArchiveInputStream
for the specifiedname
of the resource.static org.apache.commons.compress.archivers.ArchiveInputStream
getArchiveInputStream(ArchiveType archiveType, InputStream inputStream)
Returns theArchiveInputStream
for the specifiedarchiveType
.static ArchiveType
getArchiveType(String name)
Determines the archive type based on a file name.static File
getPackedFile(File file)
Adds ".gz" to the file namestatic String
getPackedFileName(String fileName)
adds ".gz" to the given file namestatic String
getParentPath(String path)
Similar to PathUtil#getParentPath(String) but support "!/" path separator and only forward slashes allowed.static boolean
isNameAllowed(String entryName)
static boolean
isPackedFile(File file)
static boolean
isPackedFileName(String fileName)
static boolean
isZipContainsEntry(File zip, String relativePath)
static byte[]
packBytes(byte[] unpacked)
static File
packFile(File srcFile)
Creates a packed file "file.ext.gz" from the given file "file.ext".static void
packFileTo(File dstFile, File srcFile)
Packs the content of the source file to the destination file.static void
packStream(OutputStream out, InputStream in)
Packs the entire content of the input stream and puts it into the output stream.static boolean
packToZip(File root, FilenameFilter filter, ZipOutputStream output, boolean closeZip)
static boolean
packZip(File destFile, FilenameFilter filter, Collection<File> sourceFiles)
Packs files and folders to a zip archive.static boolean
packZip(File root, FilenameFilter filter, ZipOutputStream output)
Same aspackZip(java.io.File, java.util.zip.ZipOutputStream)
, but allows to specify the filename filter additionally.static boolean
packZip(File destFile, Collection<File> sourceFiles)
static boolean
packZip(File root, ZipOutputStream output)
Packs a file (or a directory) to a zip archive.static boolean
packZip(OutputStream destStream, Collection<File> sourceFiles)
static void
packZip(Collection<File> sourceFiles, File resultZip, Map<String,Integer> entitiesUnixPermissions, boolean markExecutableFiles)
Assembles a zip archive setting Unix permissions for the archive entries.static List<String>
splitByArchivePathSeparator(String path, boolean withEmptyLast)
Splits path by #ARCHIVE_PATH_SEPARATOR ("!/").static byte[]
unpackBytes(byte[] packed)
static void
unpackStream(OutputStream out, InputStream in)
Unpacks the entire content of the input stream and puts it into the output stream.static boolean
unpackZip(File zip, File targetDir)
static boolean
unpackZip(File zip, String pathPrefix, File targetDir)
Extract files from zip archivestatic boolean
unpackZip(ZipInputStream input, File targetDir)
Extracts the compressed files from the zip input streaminput
and stores them in thetargetDir
.static void
unpackZipOrThrow(File zip, String pathPrefix, File targetDir, boolean clearTargetDir)
Extracts files from zip archive and throws IOException in case there is an error.-
Methods inherited from class jetbrains.buildServer.util.BaseArchiveUtil
isZipContainsFolder
-
-
-
-
Field Detail
-
DEFAULT_ZIP_ENCODING
public static final String DEFAULT_ZIP_ENCODING
- See Also:
- Constant Field Values
-
ARCHIVE_PATH_SEPARATOR
public static final String ARCHIVE_PATH_SEPARATOR
- Since:
- 8.0
- See Also:
- Constant Field Values
-
ARCHIVE_PATH_SEP_PATTERN
public static final Pattern ARCHIVE_PATH_SEP_PATTERN
-
-
Method Detail
-
getArchiveType
@NotNull public static ArchiveType getArchiveType(@NotNull String name)
Determines the archive type based on a file name.Currently the following extensions are supported:
-
.zip
,.nupkg
,.snupkg
,.sit
-
.jar
,.war
,.ear
,.apk
-
.tar.gz
,.tgz
,.tar.gzip
-
.tar
- Parameters:
name
- the file or resource name- Returns:
- archive type
-
-
getArchiveInputStream
@Nullable public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream(@NotNull ArchiveType archiveType, @NotNull InputStream inputStream) throws IOException
Returns theArchiveInputStream
for the specifiedarchiveType
.
!!!!NOTE. Calling this method for unknown file type may lead to stream leak !!!!- Parameters:
archiveType
- the type of archiveinputStream
- the input that should be read- Returns:
- the best possible
ArchiveInputStream
, ornull
if nothing matches. - Throws:
IOException
- if I/O error occurs
-
getArchiveInputStream
@Nullable public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream(@NotNull String name, @NotNull InputStream inputStream) throws IOException
Returns theArchiveInputStream
for the specifiedname
of the resource.
!!!!NOTE. Calling this method for unknown file type may lead to stream leak !!!!- Parameters:
name
- the name of the resourceinputStream
- the input that should be read- Returns:
- the best possible
ArchiveInputStream
, ornull
if nothing matches. - Throws:
IOException
- if I/O error occurs
-
unpackZip
public static boolean unpackZip(@NotNull File zip, @NotNull String pathPrefix, @NotNull File targetDir)
Extract files from zip archive- Parameters:
zip
- .zip file to extractpathPrefix
- path prefix to extract from zip. Use "" to extract all filestargetDir
- target folder to extract- Returns:
- true if operation succeeded, false otherwise
-
unpackZip
public static boolean unpackZip(@NotNull ZipInputStream input, @NotNull File targetDir)
Extracts the compressed files from the zip input streaminput
and stores them in thetargetDir
.The input is closed in the end.
- Parameters:
input
- zip input streamtargetDir
- the target directory- Returns:
- true in case of success, false otherwise
-
unpackZipOrThrow
public static void unpackZipOrThrow(@NotNull File zip, @NotNull String pathPrefix, @NotNull File targetDir, boolean clearTargetDir) throws IOException
Extracts files from zip archive and throws IOException in case there is an error.- Parameters:
zip
- .zip file to extractpathPrefix
- path prefix to extract from zip. Use "" to extract all filestargetDir
- target folder to extractclearTargetDir
- indicates whether the target dir should be cleared before the unpacking and in case of an error- Throws:
IOException
-
packZip
public static boolean packZip(@NotNull File destFile, @NotNull Collection<File> sourceFiles) throws FileNotFoundException
- Throws:
FileNotFoundException
-
packZip
public static boolean packZip(@NotNull File destFile, @Nullable FilenameFilter filter, @NotNull Collection<File> sourceFiles) throws FileNotFoundException
Packs files and folders to a zip archive. In case of directory all inner files are added recursively.- Parameters:
destFile
- destination zip filefilter
- file name filtersourceFiles
- files or folders to zip- Returns:
- true in case of success, false otherwise
- Throws:
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 reason
-
packZip
public static boolean packZip(@NotNull OutputStream destStream, @NotNull Collection<File> sourceFiles)
-
packZip
public static boolean packZip(@NotNull File root, @NotNull ZipOutputStream output)
Packs a file (or a directory) to a zip archive. In case of directory all inner files are added recursively.Files and directories have relative names, for instance, for the following tree:
foo/bar/File1 /File2 /baz/File3
the callpackZip(barDirectory, output)
creates a zip with "/File1", "/File2" and "/baz/File3" files, and callspackZip(bazDirectory, output)
andpackZip(file3, output)
both create a zip with a single file "/File3".Output stream is closed in the end.
- Parameters:
root
- root file or directoryoutput
- the zip stream to write to- Returns:
- true in case of success, false otherwise
-
packZip
public static boolean packZip(@NotNull File root, @Nullable FilenameFilter filter, @NotNull ZipOutputStream output)
Same aspackZip(java.io.File, java.util.zip.ZipOutputStream)
, but allows to specify the filename filter additionally.- Parameters:
root
- root file or directoryfilter
- file name filteroutput
- the zip stream to write to- Returns:
- true in case of success, false otherwise
-
packZip
public static void packZip(@NotNull Collection<File> sourceFiles, @NotNull File resultZip, @NotNull Map<String,Integer> entitiesUnixPermissions, boolean markExecutableFiles) throws IOException
Assembles a zip archive setting Unix permissions for the archive entries.- Parameters:
sourceFiles
- files to be archivedresultZip
- a path to the result zipentitiesUnixPermissions
- a map from an archive entry path (relative to the archive root) to an integer that represents Unix permissions in the format used byZipArchiveEntry.getUnixMode()
markExecutableFiles
- if true then files with extensions of executable files will be marked as executable- Throws:
IOException
-
packToZip
public static boolean packToZip(@NotNull File root, @Nullable FilenameFilter filter, @NotNull ZipOutputStream output, boolean closeZip)
-
packStream
public static void packStream(@NotNull OutputStream out, @NotNull InputStream in) throws IOException
Packs the entire content of the input stream and puts it into the output stream. The caller is responsible for closing both streams.- Parameters:
out
- output streamin
- input stream- Throws:
IOException
-
unpackStream
public static void unpackStream(@NotNull OutputStream out, @NotNull InputStream in) throws IOException
Unpacks the entire content of the input stream and puts it into the output stream. The caller is responsible for closing both streams.- Parameters:
out
- output streamin
- input stream- Throws:
IOException
-
packBytes
@Nullable public static byte[] packBytes(@Nullable byte[] unpacked)
-
unpackBytes
@Nullable public static byte[] unpackBytes(@Nullable byte[] packed)
-
extractEntry
@Nullable public static InputStream extractEntry(@NotNull ZipInputStream input, @NotNull String path) throws IOException
Extracts and returns the input stream for the file specified inside a zip stream. If the file cannot be found among zip entriesnull
is returned.Note: method does not close the
input
.- Parameters:
input
- the zip input streampath
- relative path of the file to extract- Returns:
- the input stream of the file (uncompressed)
- Throws:
IOException
- if I/O error occurs
-
getPackedFile
@NotNull public static File getPackedFile(@NotNull File file)
Adds ".gz" to the file name- Parameters:
file
- file- Returns:
- new file in the same location
-
getPackedFileName
@NotNull public static String getPackedFileName(@NotNull String fileName)
adds ".gz" to the given file name- Parameters:
fileName
- file name- Returns:
- fileName + ".gz"
-
isPackedFile
public static boolean isPackedFile(@NotNull File file)
-
isPackedFileName
public static boolean isPackedFileName(@NotNull String fileName)
-
packFile
@NotNull public static File packFile(@NotNull File srcFile) throws IOException
Creates a packed file "file.ext.gz" from the given file "file.ext". If the target file exists before calling this method it will be overwritten.- Parameters:
srcFile
- the given file- Returns:
- the packed file
- Throws:
IOException
- if a problem occurs during file operations
-
packFileTo
public static void packFileTo(@NotNull File dstFile, @NotNull File srcFile) throws IOException
Packs the content of the source file to the destination file. If the destination file exists it will be overwritten.- Parameters:
dstFile
- destination filesrcFile
- source file- Throws:
IOException
- if a problem occurs during file operations
-
isZipContainsEntry
public static boolean isZipContainsEntry(File zip, String relativePath) throws IOException
- Throws:
IOException
-
getArchivedFolderEntries
@NotNull public static Collection<ArchiveUtil.ArchivedFileInfo> getArchivedFolderEntries(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull String innerPathToFolder) throws IOException
Returns archived files info for entries in specified archive. Supports inner archives.- Parameters:
archive
- - a particular archive tp processinnerPathToFolder
- may contains "!/", e.g. "/", "/folder1/", "/folder/archive.zip!/", "/foo/archive.zip!/baz"- Returns:
- see above
- Throws:
IOException
- if IOException occurs- Since:
- 8.0
-
splitByArchivePathSeparator
@NotNull public static List<String> splitByArchivePathSeparator(@NotNull String path, boolean withEmptyLast)
Splits path by #ARCHIVE_PATH_SEPARATOR ("!/").- Parameters:
path
- path to processwithEmptyLast
- is should include empty string into result if path ends with "!/"- Returns:
- see above
- Since:
- 8.0
-
doInArchive
public static <T> T doInArchive(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull String path, @NotNull ArchiveUtil.DoInArchiveHandler<T> callback) throws IOException
Search for entry with specified path in archive and calls callable. Support internal archives Sample paths: "a.txt", "foo/baz.txt", "foo/bar.zip!/baz.txt"- Type Parameters:
T
- callback return type- Parameters:
archive
- - 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 found- Returns:
- what callback returns
- Throws:
FileNotFoundException
- if entry not foundIOException
- if archive processing error or callback returns error or failed to get input stream from archive- Since:
- 8.0
-
doInArchive
public static <T> T doInArchive(@NotNull ArchiveUtil.ArchiveFileDescriptor archive, @NotNull String path, @NotNull ArchiveUtil.DoInArchiveHandler2<T> callable) throws IOException
Similar to #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler) but callback MUST close Closeable elements (third argument)- Throws:
IOException
- Since:
- 8.0
-
getParentPath
@NotNull public static String getParentPath(@NotNull String path)
Similar to PathUtil#getParentPath(String) but support "!/" path separator and only forward slashes allowed.- Parameters:
path
- path to process.- Returns:
- parent path
- Since:
- 8.0
-
isNameAllowed
public static boolean isNameAllowed(@Nullable String entryName)
-
-