List of usage examples for java.util.zip ZipEntry getName
public String getName()
From source file:be.fedict.eid.applet.service.signer.odf.ODFUtil.java
/** * Read the zipped data in the ODF package and return the inputstream for a * given file / zip entry/*from w w w . j av a 2 s . c o m*/ * * @param inputStream * @param uri * @return inputstream for the file / zip entry * @throws IOException */ public static InputStream findDataInputStream(InputStream inputStream, String uri) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry; while (null != (zipEntry = zipInputStream.getNextEntry())) { if (zipEntry.getName().equals(uri)) { return zipInputStream; } } return null; }
From source file:com.aaasec.sigserv.cscommon.DocTypeIdentifier.java
/** * Guess the document format and return an appropriate document type string * * @param is An InputStream holding the document * @return "xml" if the document is an XML document or "pdf" if the document * is a PDF document, or else an error message. */// ww w . j a v a 2s .c om public static SigDocumentType getDocType(InputStream is) { InputStream input = null; try { input = new BufferedInputStream(is); input.mark(5); byte[] preamble = new byte[5]; int read = 0; try { read = input.read(preamble); input.reset(); } catch (IOException ex) { return SigDocumentType.Unknown; } if (read < 5) { return SigDocumentType.Unknown; } String preambleString = new String(preamble); byte[] xmlPreable = new byte[] { '<', '?', 'x', 'm', 'l' }; byte[] xmlUtf8 = new byte[] { -17, -69, -65, '<', '?' }; if (Arrays.equals(preamble, xmlPreable) || Arrays.equals(preamble, xmlUtf8)) { return SigDocumentType.XML; } else if (preambleString.equals("%PDF-")) { return SigDocumentType.PDF; } else if (preamble[0] == 'P' && preamble[1] == 'K') { ZipInputStream asics = new ZipInputStream(new BufferedInputStream(is)); ByteArrayOutputStream datafile = null; ByteArrayOutputStream signatures = null; ZipEntry entry; try { while ((entry = asics.getNextEntry()) != null) { if (entry.getName().equals("META-INF/signatures.p7s")) { signatures = new ByteArrayOutputStream(); IOUtils.copy(asics, signatures); signatures.close(); } else if (entry.getName().equalsIgnoreCase("META-INF/signatures.p7s")) { /* Wrong case */ // asics;Non ETSI compliant return SigDocumentType.Unknown; } else if (entry.getName().indexOf("/") == -1) { if (datafile == null) { datafile = new ByteArrayOutputStream(); IOUtils.copy(asics, datafile); datafile.close(); } else { // // asics;ASiC-S profile support only one data file return SigDocumentType.Unknown; } } } } catch (Exception ex) { // null;Invalid ASiC-S return SigDocumentType.Unknown; } if (datafile == null || signatures == null) { // asics;ASiC-S profile support only one data file with CAdES signature return SigDocumentType.Unknown; } // asics/cades return SigDocumentType.Unknown; } else if (preambleString.getBytes()[0] == 0x30) { // cades; return SigDocumentType.Unknown; } else { // null;Document format not recognized/handled return SigDocumentType.Unknown; } } finally { if (input != null) { try { input.close(); } catch (IOException e) { } } } }
From source file:com.joliciel.jochre.lexicon.TextFileLexicon.java
public static TextFileLexicon deserialize(ZipInputStream zis) { TextFileLexicon memoryBase = null;/*from www. j av a 2s . c om*/ try { ZipEntry zipEntry; if ((zipEntry = zis.getNextEntry()) != null) { LOG.debug("Scanning zip entry " + zipEntry.getName()); ObjectInputStream in = new ObjectInputStream(zis); memoryBase = (TextFileLexicon) in.readObject(); zis.closeEntry(); in.close(); } else { throw new RuntimeException("No zip entry in input stream"); } } catch (IOException ioe) { throw new RuntimeException(ioe); } catch (ClassNotFoundException cnfe) { throw new RuntimeException(cnfe); } return memoryBase; }
From source file:com.worldline.easycukes.commons.helpers.FileHelper.java
/** * Extracts the content of a zip folder in a specified directory * * @param from a {@link String} representation of the URL containing the zip * file to be unzipped//w w w .j a v a 2s . c o m * @param to the path on which the content should be extracted * @throws IOException if anything's going wrong while unzipping the content of the * provided zip folder */ public static void unzip(@NonNull String from, @NonNull String to, boolean isRemote) throws IOException { @Cleanup ZipInputStream zip = null; if (isRemote) zip = new ZipInputStream(new FileInputStream(from)); else zip = new ZipInputStream(FileHelper.class.getResourceAsStream(from)); log.debug("Extracting zip from: " + from + " to: " + to); // Extract without a container directory if exists ZipEntry entry = zip.getNextEntry(); String rootDir = "/"; if (entry != null) if (entry.isDirectory()) rootDir = entry.getName(); else { final String filePath = to + entry.getName(); // if the entry is a file, extracts it try { extractFile(zip, filePath); } catch (final FileNotFoundException fnfe) { log.warn(fnfe.getMessage(), fnfe); } } zip.closeEntry(); entry = zip.getNextEntry(); // iterates over entries in the zip file while (entry != null) { String entryName = entry.getName(); if (entryName.startsWith(rootDir)) entryName = entryName.replaceFirst(rootDir, ""); final String filePath = to + "/" + entryName; if (!entry.isDirectory()) // if the entry is a file, extracts it try { extractFile(zip, filePath); } catch (final FileNotFoundException fnfe) { log.warn(fnfe.getMessage(), fnfe); } else { // if the entry is a directory, make the directory final File dir = new File(filePath); dir.mkdir(); } zip.closeEntry(); entry = zip.getNextEntry(); } // delete the zip file if recovered from URL if (isRemote) new File(from).delete(); }
From source file:eionet.gdem.conversion.odf.OpenDocumentUtils.java
/** * Returns true, if inputstream is zip file * @param input InputStream// ww w .j a v a 2s . c om * @return True if InputStream is a zip file. */ public static boolean isSpreadsheetFile(InputStream input) { ZipInputStream zipStream = null; ZipEntry zipEntry = null; try { zipStream = new ZipInputStream(input); while (zipStream.available() == 1 && (zipEntry = zipStream.getNextEntry()) != null) { if (zipEntry != null) { if ("content.xml".equals(zipEntry.getName())) { // content file found, it is OpenDocument. return true; } } } } catch (IOException ioe) { return false; } finally { IOUtils.closeQuietly(zipStream); } return false; }
From source file:Main.java
public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException { ZipFile zfile = new ZipFile(zipFile); Enumeration<? extends ZipEntry> zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { continue; }/*from w ww .j av a2 s .c o m*/ Log.d(TAG, "ze.getName() = " + ze.getName()); OutputStream os = new BufferedOutputStream( new FileOutputStream(getRealFileName(folderPath, ze.getName()))); InputStream is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } is.close(); os.close(); } zfile.close(); return true; }
From source file:com.sangupta.jerry.util.ZipUtils.java
/** * Read a given file from the ZIP file and store it in a temporary file. The * temporary file is set to be deleted on exit of application. * //w w w .ja v a2 s . c om * @param zipFile * the zip file from which the file needs to be read * * @param fileName * the name of the file that needs to be extracted * * @return the {@link File} handle for the extracted file in the temp * directory * * @throws IllegalArgumentException * if the zipFile is <code>null</code> or the fileName is * <code>null</code> or empty. */ public static File readFileFromZip(File zipFile, String fileName) throws FileNotFoundException, IOException { if (zipFile == null) { throw new IllegalArgumentException("zip file to extract from cannot be null"); } if (AssertUtils.isEmpty(fileName)) { throw new IllegalArgumentException("the filename to extract cannot be null/empty"); } LOGGER.debug("Reading {} from {}", fileName, zipFile.getAbsolutePath()); ZipInputStream stream = null; BufferedOutputStream outStream = null; File tempFile = null; try { byte[] buf = new byte[1024]; stream = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry; while ((entry = stream.getNextEntry()) != null) { String entryName = entry.getName(); if (entryName.equals(fileName)) { tempFile = File.createTempFile(FilenameUtils.getName(entryName), FilenameUtils.getExtension(entryName)); tempFile.deleteOnExit(); outStream = new BufferedOutputStream(new FileOutputStream(tempFile)); int readBytes; while ((readBytes = stream.read(buf, 0, 1024)) > -1) { outStream.write(buf, 0, readBytes); } stream.close(); outStream.close(); return tempFile; } } } finally { IOUtils.closeQuietly(stream); IOUtils.closeQuietly(outStream); } return tempFile; }
From source file:com.googlecode.dex2jar.test.TestUtils.java
public static void checkZipFile(File zip) throws ZipException, Exception { ZipFile zipFile = new ZipFile(zip); for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) { ZipEntry entry = e.nextElement(); if (entry.getName().endsWith(".class")) { StringWriter sw = new StringWriter(); // PrintWriter pw = new PrintWriter(sw); InputStream is = zipFile.getInputStream(entry); try { verify(new ClassReader(IOUtils.toByteArray(is))); } finally { IOUtils.closeQuietly(is); }/* w w w . j a v a 2 s . c o m*/ Assert.assertTrue(sw.toString(), sw.toString().length() == 0); } } }
From source file:com.nuvolect.deepdive.probe.ApkZipUtil.java
public static boolean unzip(OmniFile zipOmni, OmniFile targetDir, ProgressStream progressStream) { String volumeId = zipOmni.getVolumeId(); String rootFolderPath = targetDir.getPath(); boolean DEBUG = true; ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream()); ZipEntry entry = null; try {//from w w w. ja v a2s. co m while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { OmniFile dir = new OmniFile(volumeId, entry.getName()); if (dir.mkdir()) { if (DEBUG) LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath()); } } else { String path = rootFolderPath + "/" + entry.getName(); OmniFile file = new OmniFile(volumeId, path); // Create any necessary directories file.getParentFile().mkdirs(); OutputStream out = file.getOutputStream(); OmniFiles.copyFileLeaveInOpen(zis, out); if (DEBUG) LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath()); progressStream.putStream("Unpacked: " + entry.getName()); } } zis.close(); } catch (IOException e) { LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e); return false; } return true; }
From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java
public static void unZipAll(File source, File destination) throws IOException { System.out.println("Unzipping - " + source.getName()); int BUFFER = 2048; ZipFile zip = new ZipFile(source); try {//from www . j av a2s . co m destination.getParentFile().mkdirs(); Enumeration zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(destination, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = null; FileOutputStream fos = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } } catch (Exception e) { System.out.println("unable to extract entry:" + entry.getName()); throw e; } finally { if (dest != null) { dest.close(); } if (fos != null) { fos.close(); } if (is != null) { is.close(); } } } else { //Create directory destFile.mkdirs(); } if (currentEntry.endsWith(".zip")) { // found a zip file, try to extract unZipAll(destFile, destinationParent); if (!destFile.delete()) { System.out.println("Could not delete zip"); } } } } catch (Exception e) { e.printStackTrace(); System.out.println("Failed to successfully unzip:" + source.getName()); } finally { zip.close(); } System.out.println("Done Unzipping:" + source.getName()); }