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 SmartAndroidSource by jaychou2012.
the class ZipUtil method iterate.
/**
* Reads the given ZIP file and executes the given action for each given
* entry.
* <p>
* For each given entry the corresponding input stream is also passed to the
* action. If you want to stop the loop then throw a ZipBreakException.
*
* @param zip
* input ZIP file.
* @param entryNames
* names of entries to iterate
* @param action
* action to be called for each entry.
*
* @see ZipEntryCallback
* @see #iterate(File, String[], ZipInfoCallback)
*/
public static void iterate(File zip, String[] entryNames, ZipEntryCallback action) {
ZipFile zf = null;
try {
zf = new ZipFile(zip);
for (int i = 0; i < entryNames.length; i++) {
ZipEntry e = zf.getEntry(entryNames[i]);
if (e == null) {
continue;
}
InputStream is = zf.getInputStream(e);
try {
action.process(is, e);
} catch (IOException ze) {
throw new ZipException("Failed to process zip entry '" + e.getName() + " with action " + action, ze);
} catch (ZipBreakException ex) {
break;
} finally {
IOUtils.closeQuietly(is);
}
}
} catch (IOException e) {
throw ZipExceptionUtil.rethrow(e);
} finally {
closeQuietly(zf);
}
}
use of java.util.zip.ZipFile in project SmartAndroidSource by jaychou2012.
the class Zips method iterateExistingExceptRemoved.
// ///////////// private api ///////////////
/**
* Iterate through source for not removed entries with a given callback
*
* @param zipEntryCallback
* callback to execute on entries or their info.
*/
private void iterateExistingExceptRemoved(ZipEntryOrInfoAdapter zipEntryCallback) {
if (src == null) {
// iterate.
return;
}
final Set<String> removedDirs = ZipUtil.filterDirEntries(src, removedEntries);
ZipFile zf = null;
try {
zf = getZipFile();
// manage existing entries
Enumeration<? extends ZipEntry> en = zf.entries();
while (en.hasMoreElements()) {
ZipEntry entry = en.nextElement();
String entryName = entry.getName();
if (removedEntries.contains(entryName) || isEntryInDir(removedDirs, entryName)) {
// removed entries are
continue;
}
if (nameMapper != null) {
String mappedName = nameMapper.map(entry.getName());
if (mappedName == null) {
// we should ignore this entry
continue;
} else if (!mappedName.equals(entry.getName())) {
// if name is different, do nothing
entry = ZipEntryUtil.copy(entry, mappedName);
}
}
InputStream is = zf.getInputStream(entry);
try {
zipEntryCallback.process(is, entry);
} catch (ZipBreakException ex) {
break;
} finally {
IOUtils.closeQuietly(is);
}
}
} catch (IOException e) {
ZipExceptionUtil.rethrow(e);
} finally {
ZipUtil.closeQuietly(zf);
}
}
use of java.util.zip.ZipFile in project SmartAndroidSource by jaychou2012.
the class ZipUtil method iterate.
/* Traversing ZIP files */
/**
* Reads the given ZIP file and executes the given action for each entry.
* <p>
* For each entry the corresponding input stream is also passed to the
* action. If you want to stop the loop then throw a ZipBreakException.
*
* @param zip
* input ZIP file.
* @param action
* action to be called for each entry.
*
* @see ZipEntryCallback
* @see #iterate(File, ZipInfoCallback)
*/
public static void iterate(File zip, ZipEntryCallback action) {
ZipFile zf = null;
try {
zf = new ZipFile(zip);
Enumeration<? extends ZipEntry> en = zf.entries();
while (en.hasMoreElements()) {
ZipEntry e = (ZipEntry) en.nextElement();
InputStream is = zf.getInputStream(e);
try {
action.process(is, e);
} catch (IOException ze) {
throw new ZipException("Failed to process zip entry '" + e.getName() + "' with action " + action, ze);
} catch (ZipBreakException ex) {
break;
} finally {
IOUtils.closeQuietly(is);
}
}
} catch (IOException e) {
throw ZipExceptionUtil.rethrow(e);
} finally {
closeQuietly(zf);
}
}
Aggregations