log.info( "Couldn't find " + f.getPath() ); zf = new ZipFile(f); } catch (IOException ioe) { ioe.printStackTrace() ; Enumeration entries = zf.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement(); policePartSize(f, entry.getSize(), entry.getName()); InputStream in = null; try { byte[] bytes = getBytesFromInputStream( zf.getInputStream(entry) ); policePartSize(f, bytes.length, entry.getName()); // in case earlier check ineffective partByteArrays.put(entry.getName(), new ByteArray(bytes) ); } catch (PartTooLargeException e) { throw e;
protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(out); try { Enumeration<? extends ZipArchiveEntry> en = zipEntrySource.getEntries(); while (en.hasMoreElements()) { ZipArchiveEntry ze = en.nextElement(); ZipArchiveEntry zeOut = new ZipArchiveEntry(ze.getName()); zeOut.setSize(ze.getSize()); zeOut.setTime(ze.getTime()); zos.putArchiveEntry(zeOut); try (final InputStream is = zipEntrySource.getInputStream(ze)) { if (is instanceof ZipArchiveThresholdInputStream) { XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName()); zos.closeArchiveEntry();
ZipFile zip = null; try { zip = new ZipFile(dependency.getActualFilePath()); if (zip.getEntry("META-INF/MANIFEST.MF") != null || zip.getEntry("META-INF/maven") != null) { final Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); if (!entry.isDirectory()) { final String name = entry.getName().toLowerCase(); if (name.endsWith(".class")) { isJar = true;
private boolean isTooLageForZip32(final ZipArchiveEntry zipArchiveEntry){ return zipArchiveEntry.getSize() >= ZIP64_MAGIC || zipArchiveEntry.getCompressedSize() >= ZIP64_MAGIC; }
/** * Sets Unix permissions in a way that is understood by Info-Zip's * unzip command. * @param mode an <code>int</code> value */ public void setUnixMode(final int mode) { // CheckStyle:MagicNumberCheck OFF - no point setExternalAttributes((mode << SHORT_SHIFT) // MS-DOS read-only attribute | ((mode & 0200) == 0 ? 1 : 0) // MS-DOS directory flag | (isDirectory() ? 0x10 : 0)); // CheckStyle:MagicNumberCheck ON platform = PLATFORM_UNIX; }
/** * If the mode is AsNeeded and the entry is a compressed entry of * unknown size that gets written to a non-seekable stream then * change the default to Never. * * @since 1.3 */ private Zip64Mode getEffectiveZip64Mode(final ZipArchiveEntry ze) { if (zip64Mode != Zip64Mode.AsNeeded || channel != null || ze.getMethod() != DEFLATED || ze.getSize() != ArchiveEntry.SIZE_UNKNOWN) { return zip64Mode; } return Zip64Mode.Never; }
private static MediaType detectKmz(ZipFile zip) { boolean kmlFound = false; Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String name = entry.getName(); if (!entry.isDirectory() && name.indexOf('/') == -1 && name.indexOf('\\') == -1) { if (name.endsWith(".kml") && !kmlFound) { kmlFound = true; } else { return null; } } } if (kmlFound) { return MediaType.application("vnd.google-earth.kmz"); } else { return null; } }
/** * * @param zipfile the template file * @param tmpfile the XML file with the sheet data * @param entry the name of the sheet entry to substitute, e.g. xl/worksheets/sheet1.xml * @param out the stream to write the result to */ private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException { try (ZipFile zip = ZipHelper.openZipFile(zipfile)) { try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(out)) { Enumeration<? extends ZipArchiveEntry> en = zip.getEntries(); while (en.hasMoreElements()) { ZipArchiveEntry ze = en.nextElement(); if (!ze.getName().equals(entry)) { zos.putArchiveEntry(new ZipArchiveEntry(ze.getName())); try (InputStream is = zip.getInputStream(ze)) { copyStream(is, zos); } zos.closeArchiveEntry(); } } zos.putArchiveEntry(new ZipArchiveEntry(entry)); try (InputStream is = new FileInputStream(tmpfile)) { copyStream(is, zos); } zos.closeArchiveEntry(); } } }
private static MediaType tryStreamingDetection(TikaInputStream stream) { Set<String> entryNames = new HashSet<>(); try (InputStream is = new FileInputStream(stream.getFile())) { ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is); ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry(); while (zae != null) { if (zae.isDirectory()) { zae = zipArchiveInputStream.getNextZipEntry(); continue; entryNames.add(zae.getName()); if (zae.getName().equals("[Content_Types].xml")) { MediaType mt = parseContentTypes(zipArchiveInputStream); if (mt != null) {
private static void addToArchiveCompression(ZipArchiveOutputStream out, File file, String dir) throws IOException { String name = dir + File.separator + file.getName(); if (file.isFile()) { ZipArchiveEntry entry = new ZipArchiveEntry(name); out.putArchiveEntry(entry); entry.setSize(file.length()); IOUtils.copy(new FileInputStream(file), out); out.closeArchiveEntry(); } else if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (File child : children) { addToArchiveCompression(out, child, name); } } } else { System.out.println(file.getName() + " is not supported"); } }
try zipFile = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding, true ); final Enumeration e = zipFile.getEntriesInPhysicalOrder(); while ( e.hasMoreElements() ) final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement(); final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zipFile, ze ); if ( !isSelected( ze.getName(), fileInfo ) ) if ( ze.getName().startsWith( path ) ) in = zipFile.getInputStream( ze ); ze.getName(), new Date( ze.getTime() ), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink( zipFile, ze ), getFileMappers() );
@edu.umd.cs.findbugs.annotations.SuppressWarnings( value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs") private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException { try { @Cleanup OutputStream archiveStream = new FileOutputStream(zipFile); @Cleanup ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream); for (File fileToAdd : filesToAdd) { ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName()); archive.putArchiveEntry(entry); @Cleanup BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd)); IOUtils.copy(input, archive); archive.closeArchiveEntry(); } archive.finish(); } catch (ArchiveException e) { throw new IOException("Issue with creating archive", e); } }
public static void unzip(Path in, Path targetDir, boolean skipExisting, CopyOption... options) throws IOException { try (ZipFile zip = new ZipFile(in.toFile())) { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry e = entries.nextElement(); Path p = targetDir.resolve(e.getName()); if (skipExisting && Files.exists(p)) { continue; } if (e.isDirectory()) { Files.createDirectories(p); } else { Path parent = p.getParent(); if (!Files.exists(parent)) { Files.createDirectories(parent); } try (InputStream src = zip.getInputStream(e)) { Files.copy(src, p, options); } int unixMode = e.getUnixMode(); if (unixMode <= 0) { unixMode = Posix.DEFAULT_UNIX_MODE; } Files.setPosixFilePermissions(p, Posix.posix(unixMode)); } } } }
private static InputStream getZipStream(String zipPath, ZipPackage zipPackage) throws IOException, TikaException { String targPath = (zipPath.length() > 1 && zipPath.startsWith("/") ? zipPath.substring(1) : zipPath); ZipEntrySource zipEntrySource = zipPackage.getZipArchive(); Enumeration<? extends ZipArchiveEntry> zipEntryEnumeration = zipEntrySource.getEntries(); ZipArchiveEntry zipEntry = null; while (zipEntryEnumeration.hasMoreElements()) { ZipArchiveEntry ze = zipEntryEnumeration.nextElement(); if (ze.getName().equals(targPath)) { zipEntry = ze; break; } } if (zipEntry == null) { throw new TikaException("Couldn't find required zip entry: " + zipPath); } return zipEntrySource.getInputStream(zipEntry); } }
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName); FileInputStream fis = null; ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); try { fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length);
private static void repairCopy(File brokenZip, File fixedZip) { try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(fixedZip)) { try (InputStream is = new FileInputStream(brokenZip)) { ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is); ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry(); while (zae != null) { try { if (!zae.isDirectory() && zipArchiveInputStream.canReadEntryData(zae)) { outputStream.putArchiveEntry(zae); outputStream.flush(); outputStream.closeArchiveEntry(); if (!successfullyCopied) {
progressListener.start(progressLabel); FileInputStream fin = new FileInputStream(source); BufferedInputStream in = new BufferedInputStream(fin); ZipArchiveInputStream zipIn = new ZipArchiveInputStream(in); try { ZipArchiveEntry entry; while ((entry = zipIn.getNextZipEntry()) != null) { if (file.matcher(entry.getName()).matches()) { if (zipIn.canReadEntryData(entry)) { long size = entry.getSize(); Files.write(zipIn, size, destination); destination.setExecutable(true);
public ZipArchiveEntry nextEntry() { while (hasNext()) { currentEntry = entries.nextElement(); File entryFile = new File(currentEntry.getName()); if (filter.accept(entryFile)) { return currentEntry; } } throw new NoSuchElementException(); }
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); ZipArchiveInputStream zis = new ZipArchiveInputStream(is); FileOutputStream fos = new FileOutputStream(tmpFile); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); while ((ze = zis.getNextZipEntry()) != null) { ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName()); zeNew.setComment(ze.getComment()); zeNew.setExtra(ze.getExtra()); zeNew.setTime(ze.getTime()); zos.putArchiveEntry(zeNew); FilterOutputStream fos2 = new FilterOutputStream(zos) { cos.close(); fos2.close(); zos.closeArchiveEntry(); zos.close(); fos.close(); zis.close();
public void saveContentTypes(ContentTypeManager ctm) throws Docx4JException { try { zos.putArchiveEntry(new ZipArchiveEntry("[Content_Types].xml")); ctm.marshal(zos); zos.closeArchiveEntry(); } catch (Exception e) { throw new Docx4JException("Error marshalling Content_Types ", e); } }