use of java.util.zip.ZipInputStream in project atlas by alibaba.
the class JarSplitUtils method splitZip.
public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException {
if (outputFile.exists()) {
FileUtils.deleteQuietly(outputFile);
}
if (null == includeEnties || includeEnties.size() < 1) {
return;
}
FileOutputStream fos = new FileOutputStream(outputFile);
ZipOutputStream jos = new ZipOutputStream(fos);
final byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(inputFile);
ZipInputStream zis = new ZipInputStream(fis);
try {
// loop on the entries of the jar file package and put them in the final jar
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do not take directories or anything inside a potential META-INF folder.
if (entry.isDirectory()) {
continue;
}
String name = entry.getName();
if (!includeEnties.contains(name)) {
continue;
}
JarEntry newEntry;
// Preserve the STORED method of the input entry.
if (entry.getMethod() == JarEntry.STORED) {
newEntry = new JarEntry(entry);
} else {
// Create a new entry so that the compressed len is recomputed.
newEntry = new JarEntry(name);
}
// add the entry to the jar archive
jos.putNextEntry(newEntry);
// read the content of the entry from the input stream, and write it into the archive.
int count;
while ((count = zis.read(buffer)) != -1) {
jos.write(buffer, 0, count);
}
// close the entries for this file
jos.closeEntry();
zis.closeEntry();
}
} finally {
zis.close();
}
fis.close();
jos.close();
}
use of java.util.zip.ZipInputStream in project atlas by alibaba.
the class ZipUtils method addFileToZipFile.
/**
* 增加文件到zip文件
*
* @param zipFile
* @param file
* @param destPath 要放的路径
* @param overwrite 是否覆盖
* @throws IOException
*/
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath, boolean overwrite) throws IOException {
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
ZipEntry entry = zin.getNextEntry();
boolean addFile = true;
while (entry != null) {
boolean addEntry = true;
String name = entry.getName();
if (StringUtils.equalsIgnoreCase(name, destPath)) {
if (overwrite) {
addEntry = false;
} else {
addFile = false;
}
}
if (addEntry) {
ZipEntry zipEntry = null;
if (ZipEntry.STORED == entry.getMethod()) {
zipEntry = new ZipEntry(entry);
} else {
zipEntry = new ZipEntry(name);
}
out.putNextEntry(zipEntry);
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
if (addFile) {
InputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
ZipEntry zipEntry = new ZipEntry(destPath);
out.putNextEntry(zipEntry);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Close the streams
zin.close();
out.close();
}
use of java.util.zip.ZipInputStream in project hudson-2.x by hudson.
the class FilePath method unzip.
private void unzip(File dir, InputStream in) throws IOException {
// without absolutization, getParentFile below seems to fail
dir = dir.getAbsoluteFile();
ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
java.util.zip.ZipEntry e;
try {
while ((e = zip.getNextEntry()) != null) {
File f = new File(dir, e.getName());
if (e.isDirectory()) {
f.mkdirs();
} else {
File p = f.getParentFile();
if (p != null)
p.mkdirs();
IOUtils.copy(zip, f);
f.setLastModified(e.getTime());
zip.closeEntry();
}
}
} finally {
zip.close();
}
}
use of java.util.zip.ZipInputStream in project SmartAndroidSource by jaychou2012.
the class ZipUtil method iterate.
/**
* Reads the given ZIP stream 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 is
* input ZIP stream (it will not be closed automatically).
* @param action
* action to be called for each entry.
*
* @see ZipEntryCallback
* @see #iterate(File, ZipEntryCallback)
*/
public static void iterate(InputStream is, ZipEntryCallback action, Charset charset) {
try {
ZipInputStream in = null;
if (charset == null) {
in = new ZipInputStream(new BufferedInputStream(is));
} else {
in = ZipFileUtil.createZipInputStream(is, charset);
}
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
try {
action.process(in, entry);
} catch (IOException ze) {
throw new ZipException("Failed to process zip entry '" + entry.getName() + " with action " + action, ze);
} catch (ZipBreakException ex) {
break;
}
}
} catch (IOException e) {
throw ZipExceptionUtil.rethrow(e);
}
}
use of java.util.zip.ZipInputStream in project android-sqlite-asset-helper by jgilfelt.
the class Utils method getFileFromZip.
public static ZipInputStream getFileFromZip(InputStream zipFileStream) throws IOException {
ZipInputStream zis = new ZipInputStream(zipFileStream);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
Log.w(TAG, "extracting file: '" + ze.getName() + "'...");
return zis;
}
return null;
}
Aggregations