use of java.util.zip.ZipFile in project Lazy by l123456789jy.
the class ZipUtil method upZipFile.
/**
* 解压缩一个文件
*
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
*/
public static void upZipFile(File zipFile, String folderPath) {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = null;
try {
zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte[] buffer = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of java.util.zip.ZipFile in project Lazy by l123456789jy.
the class ZipUtil method zipFiles.
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param zipListener zipListener
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, ZipListener zipListener) {
ZipOutputStream zipout = null;
try {
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
if (stopZipFlag) {
break;
}
zipFile(resFile, zipout, "", zipListener);
}
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of java.util.zip.ZipFile in project dex-method-counts by mihaip.
the class Main method openDexFile.
RandomAccessFile openDexFile(ZipFile zipFile, ZipEntry entry) throws IOException {
// We know it's a zip; see if there's anything useful inside. A
// failure here results in some type of IOException (of which
// ZipException is a subclass).
InputStream zis = zipFile.getInputStream(entry);
// Create a temp file to hold the DEX data, open it, and delete it
// to ensure it doesn't hang around if we fail.
File tempFile = File.createTempFile("dexdeps", ".dex");
RandomAccessFile dexFile = new RandomAccessFile(tempFile, "rw");
tempFile.delete();
// Copy all data from input stream to output file.
byte[] copyBuf = new byte[32768];
int actual;
while (true) {
actual = zis.read(copyBuf);
if (actual == -1)
break;
dexFile.write(copyBuf, 0, actual);
}
dexFile.seek(0);
return dexFile;
}
use of java.util.zip.ZipFile in project dex-method-counts by mihaip.
the class Main method openInputFileAsZip.
/**
* Tries to open an input file as a Zip archive (jar/apk) with a
* "classes.dex" inside.
*/
void openInputFileAsZip(String fileName, List<RandomAccessFile> dexFiles) throws IOException {
ZipFile zipFile;
// Try it as a zip file.
try {
zipFile = new ZipFile(fileName);
} catch (FileNotFoundException fnfe) {
// not found, no point in retrying as non-zip.
System.err.println("Unable to open '" + fileName + "': " + fnfe.getMessage());
throw fnfe;
} catch (ZipException ze) {
// not a zip
return;
}
// Open and add all files matching "classes.*\.dex" in the zip file.
for (ZipEntry entry : Collections.list(zipFile.entries())) {
if (entry.getName().matches("classes.*\\.dex")) {
dexFiles.add(openDexFile(zipFile, entry));
}
}
zipFile.close();
}
use of java.util.zip.ZipFile in project jodd by oblac.
the class ZipUtilTest method testZip.
@Test
public void testZip() throws IOException {
ZipUtil.zip(new File(dataRoot, "sb.data"));
File zipFile = new File(dataRoot, "sb.data.zip");
assertTrue(zipFile.exists());
// cleanup
FileUtil.delete(zipFile);
ZipUtil.zip(new File(dataRoot, "file"));
zipFile = new File(dataRoot, "file.zip");
assertTrue(zipFile.exists());
// cleanup
FileUtil.delete(zipFile);
}
Aggregations