jetbrains.buildServer.util
Class ArchiveUtil

java.lang.Object
  extended by jetbrains.buildServer.util.ArchiveUtil

public abstract class ArchiveUtil
extends java.lang.Object

Provides utils to archive and extract files from zip archive Date: 15.05.2008 Time: 8:25:20


Field Summary
static java.lang.String DEFAULT_ZIP_ENCODING
           
 
Constructor Summary
ArchiveUtil()
           
 
Method Summary
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 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 boolean isPackedFile(java.io.File file)
           
static boolean isPackedFileName(java.lang.String fileName)
           
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 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 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.lang.String pathPrefix, java.io.File targetDir)
          Extract files from zip archive
static boolean unpackZip(java.util.zip.ZipInputStream input, java.io.File targetDir)
          Extracts the compressed files from the zip input stream input and stores them in the targetDir.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ZIP_ENCODING

public static final java.lang.String DEFAULT_ZIP_ENCODING
See Also:
Constant Field Values
Constructor Detail

ArchiveUtil

public ArchiveUtil()
Method Detail

getArchiveType

@NotNull
public static ArchiveType getArchiveType(@NotNull
                                                 java.lang.String name)
Determines the archive type based on a file name.

Currently the following extensions are supported:

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
                                                                                                      java.io.InputStream inputStream)
                                                                                      throws java.io.IOException
Returns the ArchiveInputStream for the specified archiveType.

Parameters:
archiveType - the type of archive
inputStream - the input that should be read
Returns:
the best possible ArchiveInputStream, or null if nothing matches.
Throws:
java.io.IOException - if I/O error occurs

getArchiveInputStream

@Nullable
public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream(@NotNull
                                                                                                      java.lang.String name,
                                                                                                      @NotNull
                                                                                                      java.io.InputStream inputStream)
                                                                                      throws java.io.IOException
Returns the ArchiveInputStream for the specified name of the resource.

Parameters:
name - the name of the resource
inputStream - the input that should be read
Returns:
the best possible ArchiveInputStream, or null if nothing matches.
Throws:
java.io.IOException - if I/O error occurs

unpackZip

public static boolean unpackZip(@NotNull
                                java.io.File zip,
                                @NotNull
                                java.lang.String pathPrefix,
                                @NotNull
                                java.io.File targetDir)
Extract files from zip archive

Parameters:
zip - .zip file to extract
pathPrefix - path prefix to extract from zip. Use "" to extract all files
targetDir - target folder to extract
Returns:
true if operation succeeded, false otherwise

unpackZip

public static boolean unpackZip(@NotNull
                                java.util.zip.ZipInputStream input,
                                @NotNull
                                java.io.File targetDir)
Extracts the compressed files from the zip input stream input and stores them in the targetDir.

The input is closed in the end.

Parameters:
input - zip input stream
targetDir - the target directory
Returns:
true in case of success, false otherwise

packZip

public static boolean packZip(java.io.File root,
                              java.util.zip.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 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.

Parameters:
root - root file or directory
output - the zip stream to write to
Returns:
true in case of success, false otherwise

packZip

public 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.

Parameters:
root - root file or directory
filter - file name filter
output - the zip stream to write to
Returns:
true in case of success, false otherwise

packStream

public static void packStream(@NotNull
                              java.io.OutputStream out,
                              @NotNull
                              java.io.InputStream in)
                       throws java.io.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 stream
in - input stream
Throws:
java.io.IOException

unpackStream

public static void unpackStream(@NotNull
                                java.io.OutputStream out,
                                @NotNull
                                java.io.InputStream in)
                         throws java.io.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 stream
in - input stream
Throws:
java.io.IOException

packBytes

@Nullable
public static byte[] packBytes(byte[] unpacked)

unpackBytes

@Nullable
public static byte[] unpackBytes(byte[] packed)

extractEntry

@Nullable
public static java.io.InputStream extractEntry(@NotNull
                                                        java.util.zip.ZipInputStream input,
                                                        @NotNull
                                                        java.lang.String path)
                                        throws java.io.IOException
Extracts and returns the input stream for the file specified inside a zip stream. If the file cannot be found among zip entries null is returned.

Note: method does not close the input.

Parameters:
input - the zip input stream
path - relative path of the file to extract
Returns:
the input stream of the file (uncompressed)
Throws:
java.io.IOException - if I/O error occurs

getPackedFile

@NotNull
public static java.io.File getPackedFile(@NotNull
                                                 java.io.File file)
Adds ".gz" to the file name

Parameters:
file - file
Returns:
new file in the same location

getPackedFileName

@NotNull
public static java.lang.String getPackedFileName(@NotNull
                                                         java.lang.String fileName)
adds ".gz" to the given file name

Parameters:
fileName - file name
Returns:
fileName + ".gz"

isPackedFile

public static boolean isPackedFile(@NotNull
                                   java.io.File file)

isPackedFileName

public static boolean isPackedFileName(@NotNull
                                       java.lang.String fileName)

packFile

@NotNull
public static java.io.File packFile(@NotNull
                                            java.io.File srcFile)
                             throws java.io.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:
java.io.IOException - if a problem occurs during file operations

packFileTo

public static void packFileTo(java.io.File dstFile,
                              java.io.File srcFile)
                       throws java.io.IOException
Packs the content of the source file to the destination file. If the destination file exists it will be overwritten.

Parameters:
dstFile - destination file
srcFile - source file
Throws:
java.io.IOException - if a problem occurs during file operations