use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method unzip.
public static List<String> unzip(final File zipFile, final String destination, String encoding, Map<String, ZipEntry> zipEntryMethodMap, boolean isRelativePath) {
List<String> fileNames = new ArrayList<String>();
String dest = destination;
if (!destination.endsWith(File.separator)) {
dest = destination + File.separator;
}
ZipFile file;
try {
file = null;
if (null == encoding)
file = new ZipFile(zipFile);
else
file = new ZipFile(zipFile, encoding);
Enumeration<ZipArchiveEntry> en = file.getEntries();
ZipArchiveEntry ze = null;
while (en.hasMoreElements()) {
ze = en.nextElement();
File f = new File(dest, ze.getName());
if (ze.isDirectory()) {
f.mkdirs();
continue;
} else {
f.getParentFile().mkdirs();
InputStream is = file.getInputStream(ze);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os);
is.close();
os.close();
fileNames.add(f.getAbsolutePath());
if (zipEntryMethodMap != null && ze.getMethod() == STORED) {
zipEntryMethodMap.put(isRelativePath ? ze.getName() : f.getAbsolutePath(), ze);
}
}
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileNames;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method removeZipEntry.
public static boolean removeZipEntry(File file, Pattern pattern, File targetFile) throws FileNotFoundException, IOException {
byte[] buffer = new byte[1024];
java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)));
BufferedOutputStream bo = new BufferedOutputStream(out);
InputStream inputStream;
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
String name = zipEntry.getName();
if (pattern.matcher(name).find()) {
continue;
}
out.putNextEntry(zipEntry);
inputStream = zipFile.getInputStream(zipEntry);
write(inputStream, out, buffer);
bo.flush();
}
closeQuitely(zipFile);
closeQuitely(out);
closeQuitely(bo);
return true;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method listZipEntries.
public static List<String> listZipEntries(File zipFile) {
List<String> list = new ArrayList<String>();
ZipFile zip;
try {
zip = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> en = zip.getEntries();
ZipArchiveEntry ze = null;
while (en.hasMoreElements()) {
ze = en.nextElement();
String name = ze.getName();
name = StringUtils.replace(name, "/", ".");
if (name.endsWith(".class")) {
list.add(name);
}
}
if (null != zip)
ZipFile.closeQuietly(zip);
} catch (IOException e) {
}
return list;
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class PatchUtils method isBundleFile.
/**
* 判断当前so文件是否为bundle
*
* @param soFile
* @return
*/
public static boolean isBundleFile(File soFile) {
ZipFile zf = null;
try {
zf = new ZipFile(soFile);
Enumeration<ZipArchiveEntry> en = zf.getEntries();
if (en.hasMoreElements()) {
for (String name : BUNDLE_FILES) {
ZipArchiveEntry zipArchiveEntry = zf.getEntry(name);
if (null == zipArchiveEntry) {
return false;
}
}
return true;
}
return false;
} catch (IOException e) {
return false;
} finally {
if (null != zf) {
try {
zf.close();
} catch (IOException e) {
}
}
}
}
use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.
the class ZipUtils method unzip.
/**
* <p>
* unzip.
* </p>
*
* @param zipFile a {@link File} object.
* @param destination a {@link String} object.
* @param encoding a {@link String} object.
* @return a {@link List} object.
*/
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
List<String> fileNames = new ArrayList<String>();
String dest = destination;
if (!destination.endsWith(File.separator)) {
dest = destination + File.separator;
}
ZipFile file;
try {
file = null;
if (null == encoding)
file = new ZipFile(zipFile);
else
file = new ZipFile(zipFile, encoding);
Enumeration<ZipArchiveEntry> en = file.getEntries();
ZipArchiveEntry ze = null;
while (en.hasMoreElements()) {
ze = en.nextElement();
File f = new File(dest, ze.getName());
if (ze.isDirectory()) {
f.mkdirs();
continue;
} else {
f.getParentFile().mkdirs();
InputStream is = file.getInputStream(ze);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os);
is.close();
os.close();
fileNames.add(f.getAbsolutePath());
}
}
file.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return fileNames;
}
Aggregations