use of java.util.zip.ZipInputStream in project OpenMEAP by OpenMEAP.
the class LocalStorageImpl method unzipImportArchive.
public void unzipImportArchive(UpdateStatus update) throws LocalStorageException {
// at this point, we've verified that:
// 1) we have enough space on the device
// 2) the archive downloaded is what was expected
ZipInputStream zis = null;
String newPrefix = "com.openmeap.storage." + update.getUpdateHeader().getHash().getValue();
File hashHolder = null;
String hashRootAbsolutePath = "";
try {
hashHolder = new File(activity.getFilesDir(), newPrefix);
hashHolder.mkdir();
hashRootAbsolutePath = hashHolder.getAbsolutePath();
} catch (Exception e) {
System.out.println("Exception thrown while creating hash folder.");
System.out.println(e);
}
try {
zis = new ZipInputStream(getImportArchiveInputStream());
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
// continue;
try {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Writing directory structure in phone memory.");
File directoryStructure = new File(hashRootAbsolutePath, ze.getName());
directoryStructure.mkdirs();
} catch (Exception e) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Exception thrown while writing directory structure.");
System.out.println(e);
}
} else {
try {
String osSeperator = System.getProperty("file.separator");
int seperatorLastIndex = ze.getName().lastIndexOf(osSeperator);
String fileName = ze.getName().substring(seperatorLastIndex + 1, ze.getName().length());
String fileNameParentDirectoryPrefix = "";
String absolutePathFromPrefix = "";
if (seperatorLastIndex != -1 && seperatorLastIndex != 0) {
fileNameParentDirectoryPrefix = ze.getName().substring(0, seperatorLastIndex);
absolutePathFromPrefix = hashRootAbsolutePath + osSeperator + fileNameParentDirectoryPrefix;
} else {
absolutePathFromPrefix = hashRootAbsolutePath + osSeperator;
}
URI osResourePathForThisFile = URI.create(absolutePathFromPrefix);
File writableFileReference = new File(osResourePathForThisFile.getPath(), fileName);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(writableFileReference.getAbsolutePath(), true), 1024);
try {
byte[] buffer = new byte[1024];
int count;
while ((count = zis.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
} catch (Exception e) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Exception while writing file contents.");
System.out.println(e);
} finally {
outputStream.close();
}
} catch (Exception e) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Unknown exception.");
System.out.println(e);
}
}
// Commenting following code to make use of file:/// alternate to content://
// OutputStream baos = openFileOutputStream(newPrefix,ze.getName());
// try {
// byte[] buffer = new byte[1024];
// int count;
// while ((count = zis.read(buffer)) != -1) {
// baos.write(buffer, 0, count);
// }
// }
// catch( Exception e ) {
// ;// TODO: something, for the love of god.
// }
// finally {
// baos.close();
// }
}
} catch (Exception e) {
throw new LocalStorageException(e);
} finally {
if (zis != null) {
try {
zis.close();
} catch (IOException e) {
throw new GenericRuntimeException(e);
}
}
}
}
use of java.util.zip.ZipInputStream in project screenbird by adamhub.
the class IOUtils method isZip.
/** Return true if this file is a zip file. */
public static boolean isZip(File file) throws IOException {
if (file.exists() == false)
return false;
InputStream in = null;
try {
in = new FileInputStream(file);
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry e = zipIn.getNextEntry();
if (e == null)
return false;
int ctr = 0;
while (e != null && ctr < 4) {
e = zipIn.getNextEntry();
ctr++;
}
return true;
} catch (Throwable t) {
return false;
} finally {
try {
in.close();
} catch (Throwable t) {
}
}
}
use of java.util.zip.ZipInputStream in project screenbird by adamhub.
the class IOUtils method getZipEntry.
/** Returns an InputStream that will read a specific entry
* from a zip file.
* @param file the zip file.
* @param entryName the name of the entry.
* @return an InputStream that reads that entry.
* @throws IOException
*/
public static InputStream getZipEntry(File file, String entryName) throws IOException {
FileInputStream in = new FileInputStream(file);
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry e = zipIn.getNextEntry();
while (e != null) {
if (e.getName().equals(entryName))
return zipIn;
e = zipIn.getNextEntry();
}
return null;
}
use of java.util.zip.ZipInputStream in project atlas by alibaba.
the class JarMergerWithOverride method addJar.
public void addJar(@NonNull File file, boolean removeEntryTimestamp) throws IOException {
logger.verbose("addJar(%1$s)", file);
init();
Closer localCloser = Closer.create();
try {
FileInputStream fis = localCloser.register(new FileInputStream(file));
ZipInputStream zis = localCloser.register(new ZipInputStream(fis));
// 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();
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);
}
if (removeEntryTimestamp) {
newEntry.setTime(0);
}
// add the entry to the jar archive
logger.verbose("addJar(%1$s): entry %2$s", file, name);
duplicates.put(name, file.getAbsolutePath());
if (duplicates.get(name).size() > 1) {
logger.info("[Duplicated]" + name + ":" + file.getAbsolutePath() + ":" + duplicates.get(name));
continue;
}
jarOutputStream.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) {
jarOutputStream.write(buffer, 0, count);
}
// close the entries for this file
jarOutputStream.closeEntry();
zis.closeEntry();
}
} finally {
localCloser.close();
}
}
use of java.util.zip.ZipInputStream in project atlas by alibaba.
the class JarSplitUtils method removeFilesFromJar.
/**
* 移除指定jar文件中的class
* @param inJar
* @param removeClasses
*/
public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException {
if (null == removeClasses || removeClasses.isEmpty()) {
return;
}
File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp");
File outParentFolder = outJar.getParentFile();
if (!outParentFolder.exists()) {
outParentFolder.mkdirs();
}
FileOutputStream fos = new FileOutputStream(outJar);
ZipOutputStream jos = new ZipOutputStream(fos);
final byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(inJar);
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() || !entry.getName().endsWith(".class")) {
continue;
}
String name = entry.getName();
String className = getClassName(name);
if (removeClasses.contains(className)) {
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();
FileUtils.deleteQuietly(inJar);
FileUtils.moveFile(outJar, inJar);
}
Aggregations