List of usage examples for java.util.zip ZipEntry getName
public String getName()
From source file:ee.ria.DigiDoc.configuration.Configuration.java
private static void unpackSchema(Context context) { File schemaPath = FileUtils.getSchemaCacheDirectory(context); try (ZipInputStream zis = new ZipInputStream(context.getResources().openRawResource(R.raw.schema))) { ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { File entryFile = new File(schemaPath, ze.getName()); FileOutputStream out = new FileOutputStream(entryFile); IOUtils.copy(zis, out);/*from www. j a va2 s .com*/ out.close(); } } catch (IOException e) { Timber.e(e, "Library configuration initialization failed"); } }
From source file:Main.java
/** * Extract a zip resource into real files and directories * //w ww . ja v a 2 s. com * @param in typically given as getResources().openRawResource(R.raw.something) * @param directory target directory * @param overwrite indicates whether to overwrite existing files * @return list of files that were unpacked (if overwrite is false, this list won't include files * that existed before) * @throws IOException */ public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite) throws IOException { final int BUFSIZE = 2048; byte buffer[] = new byte[BUFSIZE]; ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE)); List<File> files = new ArrayList<File>(); ZipEntry entry; directory.mkdirs(); while ((entry = zin.getNextEntry()) != null) { File file = new File(directory, entry.getName()); files.add(file); if (overwrite || !file.exists()) { if (entry.isDirectory()) { file.mkdirs(); } else { file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE); int nRead; while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) { bos.write(buffer, 0, nRead); } bos.flush(); bos.close(); } } } zin.close(); return files; }
From source file:net.sourceforge.atunes.utils.ZipUtils.java
/** * Unzips a zip entry in a directory// w ww .j ava 2 s . co m * * @param zipfile * @param entry * @param outputDir * @throws IOException */ private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { ClosingUtils.close(outputStream); ClosingUtils.close(inputStream); } }
From source file:Main.java
public static void unzip(File zipFile, File targetDirectory) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile))); try {/*from w w w . ja va 2s .c om*/ ZipEntry ze; int count; byte[] buffer = new byte[8192]; while ((ze = zis.getNextEntry()) != null) { File file = new File(targetDirectory, ze.getName()); File dir = ze.isDirectory() ? file : file.getParentFile(); if (!dir.isDirectory() && !dir.mkdirs()) throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath()); if (ze.isDirectory()) continue; FileOutputStream fout = new FileOutputStream(file); try { while ((count = zis.read(buffer)) != -1) fout.write(buffer, 0, count); } finally { fout.close(); } } } finally { zis.close(); } }
From source file:ezbake.frack.submitter.util.JarUtil.java
public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException { JarOutputStream target = null; JarInputStream source;/*from w w w . j a v a 2 s .c om*/ File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath())); log.debug("Output file created at {}", outputJar.getAbsolutePath()); outputJar.createNewFile(); try { source = new JarInputStream(new FileInputStream(sourceJar)); target = new JarOutputStream(new FileOutputStream(outputJar)); ZipEntry entry = source.getNextEntry(); while (entry != null) { String name = entry.getName(); // Add ZIP entry to output stream. target.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; byte[] buffer = new byte[BUFFER_SIZE]; while ((len = source.read(buffer)) > 0) { target.write(buffer, 0, len); } entry = source.getNextEntry(); } source.close(); for (File fileToAdd : newFiles) { add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target); } } finally { if (target != null) { log.debug("Closing output stream"); target.close(); } } return outputJar; }
From source file:kilim.tools.FlowAnalyzer.java
public static void analyzeJar(String jarFile, Detector detector) { try {/*from ww w. j ava 2 s.c om*/ Enumeration<JarEntry> e = new JarFile(jarFile).entries(); while (e.hasMoreElements()) { ZipEntry en = (ZipEntry) e.nextElement(); String n = en.getName(); if (!n.endsWith(".class")) continue; n = n.substring(0, n.length() - 6).replace('/', '.'); analyzeClass(n, detector); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static List<File> unzip(File zip, File toDir) throws IOException { ZipFile zf = null;//w w w . ja va 2 s . c om List<File> files = null; try { zf = new ZipFile(zip); files = new ArrayList<File>(); Enumeration<?> entries = zf.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { new File(toDir, entry.getName()).mkdirs(); continue; } InputStream input = null; OutputStream output = null; try { File f = new File(toDir, entry.getName()); input = zf.getInputStream(entry); output = new FileOutputStream(f); copy(input, output); files.add(f); } finally { closeQuietly(output); closeQuietly(input); } } } finally { if (zf != null) { zf.close(); } } return files; }
From source file:Main.java
public static boolean unzipFile(InputStream fis, String destDir) { final byte[] buffer = new byte[4096]; ZipInputStream zis = null;//from w w w . ja va2 s .c om Log.e("Unzip", "destDir = " + destDir); try { // make sure the directory is existent File dstFile = new File(destDir); if (!dstFile.exists()) { dstFile.mkdirs(); } else { int fileLenght = dstFile.listFiles().length; if (fileLenght >= 2) { return true; } } zis = new ZipInputStream(fis); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { new File(destDir, fileName).mkdirs(); } else { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File(destDir, fileName))); int lenRead; while ((lenRead = zis.read(buffer)) != -1) { bos.write(buffer, 0, lenRead); } bos.close(); } zis.closeEntry(); } return true; } catch (IOException e) { e.printStackTrace(); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
From source file:com.acciente.commons.loader.ClassFinder.java
/** * Extracts a fully qualified class name from a zip file entry * @param oZipFileEntry a zip file entry * @return a fully qualified classname//w ww . j av a 2s. c om */ private static String getFQClassname(ZipEntry oZipFileEntry) { String sFileName = oZipFileEntry.getName(); if (sFileName.endsWith(".class")) { sFileName = sFileName.substring(0, sFileName.length() - ".class".length()); } return sFileName.replace('/', '.'); }
From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java
/** * Extracts the specified folder from the specified archive, into the supplied output directory. * //w w w.ja v a 2s . c om * @param archivePath * the archive Path * @param folderToExtract * the folder to extract * @param outputDirectory * the output directory * @return the extracted folder path * @throws IOException * if a problem occurs while extracting */ public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract, String outputDirectory) throws IOException { File destinationFolder = new File(outputDirectory); destinationFolder.mkdirs(); ZipFile zip = null; try { zip = new ZipFile(new File(archivePath)); Enumeration<?> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); if (currentEntry.startsWith(folderToExtract)) { File destFile = new File(destinationFolder, currentEntry); destFile.getParentFile().mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER_SIZE]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER_SIZE); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, currentByte); } } finally { if (dest != null) { dest.flush(); } IOUtils.closeQuietly(dest); IOUtils.closeQuietly(is); } } } } } finally { if (zip != null) { zip.close(); } } return new File(destinationFolder, folderToExtract); }