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 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 File extractZipToTemp(File zipFile, String nameHint)

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

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.

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

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

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

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

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