use of java.util.zip.ZipEntry in project tinker by Tencent.
the class FileUtil method unzip.
/**
* unzip
*
* @param zipFullFilename
* @param outputDirectory
* @param zipEntryNameList,if it is null or empty,will unzip all
* @return List<String>
*/
public static List<String> unzip(String zipFullFilename, String outputDirectory, List<String> zipEntryNameList) {
if (outputDirectory == null) {
throw new NullPointerException("out put directory can not be null.");
}
List<String> storeFileList = null;
ZipFile zipFile = null;
try {
storeFileList = new ArrayList<String>();
zipFile = new ZipFile(zipFullFilename);
String outputDirectoryAbsolutePath = new File(outputDirectory).getAbsolutePath();
Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = enumeration.nextElement();
String zipEntryName = zipEntry.getName();
boolean contains = false;
if (zipEntryNameList == null || zipEntryNameList.isEmpty()) {
contains = true;
} else {
if (zipEntryNameList.contains(zipEntryName)) {
contains = true;
}
}
if (contains) {
InputStream inputStream = zipFile.getInputStream(zipEntry);
String outputFullFilename = outputDirectoryAbsolutePath + Constant.Symbol.SLASH_LEFT + zipEntryName;
if (zipEntry.isDirectory()) {
createDirectory(outputFullFilename);
} else {
createFile(outputFullFilename);
OutputStream outputStream = new FileOutputStream(outputFullFilename);
try {
byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB];
int length = -1;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
outputStream.write(buffer, 0, length);
outputStream.flush();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
storeFileList.add(outputFullFilename);
}
}
}
} catch (Exception e) {
throw new FileUtilException(e);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return storeFileList;
}
use of java.util.zip.ZipEntry in project tinker by Tencent.
the class FileUtil method getZipEntryHashMap.
/**
* get zip entry hash map
*
* @param zipFile
* @return Map<String, String>
*/
private static Map<String, String> getZipEntryHashMap(String zipFullFilename) {
ZipFile zipFile = null;
Map<String, String> map = new HashMap<String, String>();
try {
zipFile = new ZipFile(zipFullFilename);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
if (!zipEntry.isDirectory()) {
String key = zipEntry.getName();
String value = zipEntry.getCrc() + Constant.Symbol.DOT + zipEntry.getSize();
map.put(key, value);
}
}
} catch (Exception e) {
throw new FileUtilException(e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
throw new FileUtilException(e);
}
}
}
return map;
}
use of java.util.zip.ZipEntry in project tinker by Tencent.
the class DexDiffDecoder method getRawOrWrappedDexMD5.
private String getRawOrWrappedDexMD5(File dexOrJarFile) {
final String name = dexOrJarFile.getName();
if (name.endsWith(".dex")) {
return MD5.getMD5(dexOrJarFile);
} else {
JarFile dexJar = null;
try {
dexJar = new JarFile(dexOrJarFile);
ZipEntry classesDex = dexJar.getEntry(DexFormat.DEX_IN_JAR_NAME);
// no code
if (classesDex == null) {
throw new TinkerPatchException(String.format("Jar file %s do not contain 'classes.dex', it is not a correct dex jar file!", dexOrJarFile.getAbsolutePath()));
}
return MD5.getMD5(dexJar.getInputStream(classesDex), 1024 * 100);
} catch (IOException e) {
throw new TinkerPatchException(String.format("File %s is not end with '.dex', but it is not a correct dex jar file !", dexOrJarFile.getAbsolutePath()), e);
} finally {
if (dexJar != null) {
try {
dexJar.close();
} catch (Exception e) {
// Ignored.
}
}
}
}
}
use of java.util.zip.ZipEntry in project tinker by Tencent.
the class FileOperation method getZipEntryMd5.
public static String getZipEntryMd5(File file, String entryName) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
return null;
}
return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
use of java.util.zip.ZipEntry in project tinker by Tencent.
the class FileUtil method mergeZip.
/**
* merge zip file
*
* @param zipOutputFullFilename
* @param mergeZipFullFilenameList
*/
public static void mergeZip(String zipOutputFullFilename, List<String> mergeZipFullFilenameList) {
FileUtil.createFile(zipOutputFullFilename);
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutputFullFilename));
if (mergeZipFullFilenameList != null) {
for (String zipFullFilename : mergeZipFullFilenameList) {
if (isExist(zipFullFilename)) {
ZipFile zipFile = new ZipFile(zipFullFilename);
Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = enumeration.nextElement();
InputStream inputStream = zipFile.getInputStream(zipEntry);
addZipEntry(zipOutputStream, zipEntry, inputStream);
}
zipFile.close();
}
}
}
} catch (Exception e) {
throw new FileUtilException(e);
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (Exception e) {
throw new FileUtilException(e);
}
}
}
Aggregations