ZipUtil

public class ZipUtil
extends Object

java.lang.Object
   ↳ com.android.tradefed.util.ZipUtil


A helper class for compression-related operations

Summary

Public constructors

ZipUtil()

Public methods

static void addToZip(ZipOutputStream out, File file, relativePathSegs)

Recursively adds given file and its contents to ZipOutputStream

static void applyPermission(File targetFile, CentralDirectoryInfo zipEntry)

Apply the file permission configured in the central directory entry.

static void closeZip(ZipFile zipFile)

Close an open ERROR(/ZipFile), ignoring any exceptions.

static File createZip(File dir)

Utility method to create a temporary zip file containing the given directory and all its contents.

static File createZip( files, String name)

Utility method to create a temporary zip file containing the given files.

static File createZip(File dir, String name)

Utility method to create a temporary zip file containing the given directory and all its contents.

static File createZip( files)

Utility method to create a temporary zip file containing the given files

static void createZip(File dir, File zipFile)

Utility method to create a zip file containing the given directory and all its contents.

static void createZip( files, File zipFile)

Utility method to create a zip file containing the given files

static File extractFileFromZip(ZipFile zipFile, String filePath)

Utility method to extract one specific file from zip file into a tmp file

static void extractZip(ZipFile zipFile, File destDir)

Utility method to extract entire contents of zip file into given directory

static void extractZip(ZipFile zipFile, File destDir, shouldExtract)

Utility method to extract contents of zip file into given directory

static File extractZipToTemp(File zipFile, String nameHint)

Extract a zip file to a temp directory prepended with a string

static getZipCentralDirectoryInfos(File partialZipFile, EndCentralDirectoryInfo endCentralDirInfo, boolean useZip64)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

static getZipCentralDirectoryInfos(File partialZipFile, EndCentralDirectoryInfo endCentralDirInfo, long offset)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

static getZipCentralDirectoryInfos(File partialZipFile, EndCentralDirectoryInfo endCentralDirInfo)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

static getZipCentralDirectoryInfos(File partialZipFile, EndCentralDirectoryInfo endCentralDirInfo, long offset, boolean useZip64)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

static void gzipFile(File file, File gzipFile)

Helper method to create a gzipped version of a single file.

static boolean isZipFileValid(File zipFile, boolean thorough)

Utility method to verify that a zip file is not corrupt.

static void unzipPartialZipFile(File partialZip, File targetFile, CentralDirectoryInfo zipEntry, LocalFileHeader localFileHeader, long startOffset)

Extract the requested file from a partial zip file.

static void unzipPartialZipFolder(File targetFile, CentralDirectoryInfo zipEntry)

Extract the requested folder from a partial zip file and apply proper permission.

Protected methods

static void validateDestinationDir(File destDir, String filename)

Public constructors

ZipUtil

public ZipUtil ()

Public methods

addToZip

public static void addToZip (ZipOutputStream out, 
                File file, 
                 relativePathSegs)

Recursively adds given file and its contents to ZipOutputStream

Parameters
out ZipOutputStream: the ERROR(/ZipOutputStream)

file File: the ERROR(/File) to add to the stream

relativePathSegs : the relative path of file, including separators

Throws
if failed to add file to zip

applyPermission

public static void applyPermission (File targetFile, 
                CentralDirectoryInfo zipEntry)

Apply the file permission configured in the central directory entry.

Parameters
targetFile File: the ERROR(/File) to set permission to.

zipEntry CentralDirectoryInfo: a CentralDirectoryInfo object that contains the file permissions.

Throws
if fail to access the file.

closeZip

public static void closeZip (ZipFile zipFile)

Close an open ERROR(/ZipFile), ignoring any exceptions.

Parameters
zipFile ZipFile: the file to close

createZip

public static File createZip (File dir)

Utility method to create a temporary zip file containing the given directory and all its contents.

Parameters
dir File: the directory to zip

Returns
File a temporary zip ERROR(/File) containing directory contents

Throws
if failed to create zip file

createZip

public static File createZip ( files, 
                String name)

Utility method to create a temporary zip file containing the given files.

Parameters
files : list of files to zip

name String: the base name of the zip file created without the extension.

Returns
File a temporary zip ERROR(/File) containing directory contents

Throws
if failed to create zip file

createZip

public static File createZip (File dir, 
                String name)

Utility method to create a temporary zip file containing the given directory and all its contents.

Parameters
dir File: the directory to zip

name String: the base name of the zip file created without the extension.

Returns
File a temporary zip ERROR(/File) containing directory contents

Throws
if failed to create zip file

createZip

public static File createZip ( files)

Utility method to create a temporary zip file containing the given files

Parameters
files : list of files to zip

Returns
File a temporary zip ERROR(/File) containing directory contents

Throws
if failed to create zip file

createZip

public static void createZip (File dir, 
                File zipFile)

Utility method to create a zip file containing the given directory and all its contents.

Parameters
dir File: the directory to zip

zipFile File: the zip file to create - it should not already exist

Throws
if failed to create zip file

createZip

public static void createZip ( files, 
                File zipFile)

Utility method to create a zip file containing the given files

Parameters
files : list of files to zip

zipFile File: the zip file to create - it should not already exist

Throws
if failed to create zip file

extractFileFromZip

public static File extractFileFromZip (ZipFile zipFile, 
                String filePath)

Utility method to extract one specific file from zip file into a tmp file

Parameters
zipFile ZipFile: the ERROR(/ZipFile) to extract

filePath String: the filePath of to extract

Returns
File the ERROR(/File) or null if not found

Throws
if failed to extract file

extractZip

public static void extractZip (ZipFile zipFile, 
                File destDir)

Utility method to extract entire contents of zip file into given directory

Parameters
zipFile ZipFile: the ERROR(/ZipFile) to extract

destDir File: the local dir to extract file to

Throws
if failed to extract file

extractZip

public static void extractZip (ZipFile zipFile, 
                File destDir, 
                 shouldExtract)

Utility method to extract contents of zip file into given directory

Parameters
zipFile ZipFile: the ERROR(/ZipFile) to extract

destDir File: the local dir to extract file to

shouldExtract : the predicate to dermine if an ZipEntry should be extracted

Throws
if failed to extract file

extractZipToTemp

public static File extractZipToTemp (File zipFile, 
                String nameHint)

Extract a zip file to a temp directory prepended with a string

Parameters
zipFile File: the zip file to extract

nameHint String: a prefix for the temp directory

Returns
File a ERROR(/File) pointing to the temp directory

getZipCentralDirectoryInfos

public static  getZipCentralDirectoryInfos (File partialZipFile, 
                EndCentralDirectoryInfo endCentralDirInfo, 
                boolean useZip64)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

Parameters
partialZipFile File: a ERROR(/File) object of the partial zip file that contains central directory entries.

endCentralDirInfo EndCentralDirectoryInfo: a EndCentralDirectoryInfo object of the zip file.

useZip64 boolean: a boolean to support zip64 format in partial download.

Returns
A list of CentralDirectoryInfo of the zip file

Throws
IOException

getZipCentralDirectoryInfos

public static  getZipCentralDirectoryInfos (File partialZipFile, 
                EndCentralDirectoryInfo endCentralDirInfo, 
                long offset)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

Parameters
partialZipFile File: a ERROR(/File) object of the partial zip file that contains central directory entries.

endCentralDirInfo EndCentralDirectoryInfo: a EndCentralDirectoryInfo object of the zip file.

offset long: the offset in the partial zip file where the content of central directory entries starts.

Returns
A list of CentralDirectoryInfo of the zip file

Throws
IOException

getZipCentralDirectoryInfos

public static  getZipCentralDirectoryInfos (File partialZipFile, 
                EndCentralDirectoryInfo endCentralDirInfo)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

Parameters
partialZipFile File: a ERROR(/File) object of the partial zip file that contains central directory entries.

endCentralDirInfo EndCentralDirectoryInfo: a EndCentralDirectoryInfo object of the zip file.

Returns
A list of CentralDirectoryInfo of the zip file

Throws
IOException

getZipCentralDirectoryInfos

public static  getZipCentralDirectoryInfos (File partialZipFile, 
                EndCentralDirectoryInfo endCentralDirInfo, 
                long offset, 
                boolean useZip64)

Get a list of {link CentralDirectoryInfo} for files in a zip file.

Parameters
partialZipFile File: a ERROR(/File) object of the partial zip file that contains central directory entries.

endCentralDirInfo EndCentralDirectoryInfo: a EndCentralDirectoryInfo object of the zip file.

offset long: the offset in the partial zip file where the content of central directory entries starts.

useZip64 boolean: a boolean to support zip64 format in partial download.

Returns
A list of CentralDirectoryInfo of the zip file

Throws
IOException

gzipFile

public static void gzipFile (File file, 
                File gzipFile)

Helper method to create a gzipped version of a single file.

Parameters
file File: the original file

gzipFile File: the file to place compressed contents in

Throws
IOException

isZipFileValid

public static boolean isZipFileValid (File zipFile, 
                boolean thorough)

Utility method to verify that a zip file is not corrupt.

Parameters
zipFile File: the ERROR(/File) to check

thorough boolean: Whether to attempt to fully extract the archive. If false, this method will fail to detect CRC errors in a well-formed archive.

Returns
boolean false if the file appears to be corrupt; true otherwise

Throws
if the file could not be opened or read

unzipPartialZipFile

public static void unzipPartialZipFile (File partialZip, 
                File targetFile, 
                CentralDirectoryInfo zipEntry, 
                LocalFileHeader localFileHeader, 
                long startOffset)

Extract the requested file from a partial zip file.

This method assumes all files are on the same disk when compressed. It doesn't support following features yet:

Zip file larger than 4GB

ZIP64(require ZipLocalFileHeader update on compressed size)

Encrypted zip file

Symlink

Parameters
partialZip File: a ERROR(/File) that's a partial of the zip file.

targetFile File: the ERROR(/File) to save the extracted file to.

zipEntry CentralDirectoryInfo: a CentralDirectoryInfo object of the file to extract from the partial zip file.

localFileHeader LocalFileHeader: a LocalFileHeader object of the file to extract from the partial zip file.

startOffset long: start offset of the file to extract.

Throws
IOException

unzipPartialZipFolder

public static void unzipPartialZipFolder (File targetFile, 
                CentralDirectoryInfo zipEntry)

Extract the requested folder from a partial zip file and apply proper permission.

Parameters
targetFile File: the ERROR(/File) to save the extracted file to.

zipEntry CentralDirectoryInfo: a CentralDirectoryInfo object of the file to extract from the partial zip file.

Throws
IOException

Protected methods

validateDestinationDir

protected static void validateDestinationDir (File destDir, 
                String filename)

Parameters
destDir File

filename String