List of usage examples for java.util.zip ZipInputStream ZipInputStream
public ZipInputStream(InputStream in)
From source file:Main.java
/** * Reads the in_stream and extracts them to out_dir. * @param in_stream Input stream corresponding to the zip file. * @param out_dir Output directory for the zip file contents. * @throws IOException/* w ww.ja v a 2 s .c o m*/ */ public static void zipExtract(InputStream in_stream, File out_dir) throws IOException { if (!out_dir.exists()) { if (!out_dir.mkdirs()) { throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath()); } } ZipInputStream zis = new ZipInputStream(in_stream); ZipEntry ze; byte[] buffer = new byte[BUFFER_SIZE]; int count; while ((ze = zis.getNextEntry()) != null) { if (ze.isDirectory()) { File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName()); fmd.mkdirs(); continue; } FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName()); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); } zis.close(); }
From source file:ZipSocket.java
public InputStream getInputStream() throws IOException { if (in == null) { in = new ZipInputStream(super.getInputStream()); }/*from w ww.j av a 2 s .c o m*/ return in; }
From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java
protected static Object fromBase64(String base64String) { try {/* w w w. j a v a 2s. com*/ ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(base64String)); try { ZipInputStream zip = new ZipInputStream(bis); try { zip.getNextEntry(); ObjectInput in = new ObjectInputStream(zip); try { return in.readObject(); } finally { in.close(); } } finally { zip.close(); } } finally { bis.close(); } } catch (ClassNotFoundException e) { log.log((Level.SEVERE), "ClassNotFoundException while deserializing member", e); return null; } catch (IOException e) { log.log((Level.SEVERE), "IOException while deserializing member", e); return null; } }
From source file:com.frostwire.util.ZipUtils.java
public static boolean unzip(File zipFile, File outputDir, ZipListener listener) { boolean result = false; try {/*from w w w . ja v a2s . c o m*/ FileUtils.deleteDirectory(outputDir); ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); try { unzipEntries(outputDir, zis, getItemCount(zipFile), System.currentTimeMillis(), listener); } finally { zis.close(); } result = true; } catch (IOException e) { LOG.error("Unable to uncompress " + zipFile + " to " + outputDir, e); result = false; } return result; }
From source file:com.BibleQuote.utils.FsUtils.java
public static BufferedReader getTextFileReaderFromZipArchive(String archivePath, String textFileInArchive, String textFileEncoding) throws FileAccessException { File zipFile = new File(archivePath); try {/*from w ww . j a va 2 s.com*/ InputStream moduleStream = new FileInputStream(zipFile); ZipInputStream zStream = new ZipInputStream(moduleStream); ZipEntry entry; while ((entry = zStream.getNextEntry()) != null) { String entryName = entry.getName().toLowerCase(); if (entryName.contains(File.separator)) { entryName = entryName.substring(entryName.lastIndexOf(File.separator) + 1); } String fileName = textFileInArchive.toLowerCase(); if (entryName.equals(fileName)) { InputStreamReader iReader = new InputStreamReader(zStream, textFileEncoding); return new BufferedReader(iReader); } ; } String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive, archivePath); Log.e(TAG, message); throw new FileAccessException(message); } catch (UTFDataFormatException e) { String message = String.format("Archive %1$s contains the file names not in the UTF format", zipFile.getName()); Log.e(TAG, message); throw new FileAccessException(message); } catch (FileNotFoundException e) { String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive, archivePath); throw new FileAccessException(message); } catch (IOException e) { Log.e(TAG, String.format("getTextFileReaderFromZipArchive(%1$s, %2$s, %3$s)", archivePath, textFileInArchive, textFileEncoding), e); throw new FileAccessException(e); } }
From source file:ZipSocket.java
public InputStream getInputStream() throws IOException { if (in == null) { in = new ZipInputStream(super.getInputStream()); }//w ww . j av a2s . co m return in; }
From source file:com.formkiq.core.util.Zips.java
/** * Extract Zip file to Map./*from w ww . j a va 2 s . c om*/ * @param bytes byte[] * @return {@link Map} * @throws IOException IOException */ public static Map<String, byte[]> extractZipToMap(final byte[] bytes) throws IOException { Map<String, byte[]> map = new HashMap<>(); ByteArrayInputStream is = new ByteArrayInputStream(bytes); ZipInputStream zipStream = new ZipInputStream(is); try { ZipEntry entry = null; while ((entry = zipStream.getNextEntry()) != null) { String filename = entry.getName(); byte[] data = IOUtils.toByteArray(zipStream); map.put(filename, data); } } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(zipStream); } return map; }
From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java
public static void unzipFile(File destFolder, File zipFile) throws IOException { ZipInputStream zipStream = null; try {//from w w w . jav a 2 s. c om if (!destFolder.exists()) { destFolder.mkdirs(); } BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile)); zipStream = new ZipInputStream(in); ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { // get output file String name = entry.getName(); if (name.startsWith("/") || name.startsWith("\\")) name = name.substring(1); File file = new File(destFolder, name); // ensure directory exists File dir = file.getParentFile(); if (!dir.exists()) dir.mkdirs(); IOUtils.copy(zipStream, new FileOutputStream(file)); } } finally { if (zipStream != null) zipStream.close(); } }
From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java
public static void unzip(String zipname) throws IOException { FileInputStream fis = new FileInputStream(zipname); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); //?? GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis)); // get directory of the zip file if (zipname.contains("\\")) zipname = zipname.replace("\\", "/"); // String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ; String zipDirectory = zipname.substring(0, zipname.lastIndexOf(".")); new File(zipDirectory).mkdir(); RunData.getInstance().setMetadataDirectory(zipDirectory); ZipEntry entry;/*from ww w .j a v a2s. co m*/ while ((entry = zis.getNextEntry()) != null) { System.out.println("Unzipping: " + entry.getName()); if (entry.getName().contains("metadata")) { RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName()); } else if (entry.getName().contains("schemes")) { RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName()); } else if (entry.getName().contains("access")) { RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName()); } int size; byte[] buffer = new byte[2048]; FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); } zis.close(); fis.close(); }
From source file:ZipFileIO.java
/** * Extract zip file to destination folder. * /*from ww w. j av a 2s . com*/ * @param file * zip file to extract * @param destination * destinatin folder */ public static void extract(File file, File destination) throws IOException { ZipInputStream in = null; OutputStream out = null; try { // Open the ZIP file in = new ZipInputStream(new FileInputStream(file)); // Get the first entry ZipEntry entry = null; while ((entry = in.getNextEntry()) != null) { String outFilename = entry.getName(); // Open the output file if (entry.isDirectory()) { new File(destination, outFilename).mkdirs(); } else { out = new FileOutputStream(new File(destination, outFilename)); // Transfer bytes from the ZIP file to the output file byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Close the stream out.close(); } } } finally { // Close the stream if (in != null) { in.close(); } if (out != null) { out.close(); } } }