List of usage examples for java.util.zip ZipEntry setSize
public void setSize(long size)
From source file:io.fabric8.maven.generator.springboot.SpringBootGenerator.java
private ZipEntry createZipEntry(File file, String fullPath) throws IOException { ZipEntry entry = new ZipEntry(fullPath); byte[] buffer = new byte[8192]; int bytesRead = -1; try (InputStream is = new FileInputStream(file)) { CRC32 crc = new CRC32(); int size = 0; while ((bytesRead = is.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); size += bytesRead;//ww w.j a v a2 s.com } entry.setSize(size); entry.setCompressedSize(size); entry.setCrc(crc.getValue()); entry.setMethod(ZipEntry.STORED); return entry; } }
From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files) throws IOException { String UNK_DIRNAME = getUNK_DIRNAME(); File unknownFileDir = new File(appDir, UNK_DIRNAME); // loop through unknown files for (Map.Entry<String, String> unknownFileInfo : files.entrySet()) { File inputFile = new File(unknownFileDir, unknownFileInfo.getKey()); if (inputFile.isDirectory()) { continue; }/*from w w w . j a v a2 s . c o m*/ ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey()); int method = Integer.valueOf(unknownFileInfo.getValue()); LogHelper.fine( String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method)); if (method == ZipEntry.STORED) { newEntry.setMethod(ZipEntry.STORED); newEntry.setSize(inputFile.length()); newEntry.setCompressedSize(-1); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile)); CRC32 crc = calculateCrc(unknownFile); newEntry.setCrc(crc.getValue()); // LogHelper.getLogger().fine("\tsize: " + newEntry.getSize()); } else { newEntry.setMethod(ZipEntry.DEFLATED); } outputFile.putNextEntry(newEntry); BrutIO.copy(inputFile, outputFile); outputFile.closeEntry(); } }
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * Creates a zip archive of the currently serialized files in * getCurrentDirectory(), placing the archive in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception * has an informative message and wraps the * originally thrown exception as root cause. * @see #getCurrentDirectory()// w ww .j a v a 2 s.c om * @see #getArchiveDirectory() */ public void archiveCurrentDirectory() throws RuntimeException { System.out.println("Making zip archive of files in " + getCurrentDirectory() + ", putting it in " + getArchiveDirectory() + "."); File current = new File(getCurrentDirectory()); if (!current.exists() || !current.isDirectory()) { throw new IllegalArgumentException("There is no " + current.getAbsolutePath() + " directory. " + "\nThis is where the serialized classes should be. " + "Please run serializeCurrentDirectory() first."); } File archive = new File(getArchiveDirectory()); if (archive.exists() && !archive.isDirectory()) { throw new IllegalArgumentException( "Output directory " + archive.getAbsolutePath() + " is not a directory."); } if (!archive.exists()) { boolean success = archive.mkdirs(); } String[] filenames = current.list(); // Create a buffer for reading the files byte[] buf = new byte[1024]; try { String version = Version.currentRepositoryVersion().toString(); // Create the ZIP file String outFilename = "serializedclasses-" + version + ".zip"; File _file = new File(getArchiveDirectory(), outFilename); FileOutputStream fileOut = new FileOutputStream(_file); ZipOutputStream out = new ZipOutputStream(fileOut); // Compress the files for (String filename : filenames) { File file = new File(current, filename); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(filename); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("Finished writing zip file " + outFilename + "."); } catch (IOException e) { throw new RuntimeException("There was an I/O error associated with " + "the process of zipping up files in " + getCurrentDirectory() + ".", e); } }
From source file:edu.cmu.tetradapp.util.TetradSerializableUtils.java
/** * Creates a zip archive of the currently serialized files in * getCurrentDirectory(), placing the archive in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception * has an informative message and wraps the * originally thrown exception as root cause. * @see #getCurrentDirectory()/*from w ww. j a v a 2 s . c o m*/ * @see #getArchiveDirectory() */ public void archiveCurrentDirectory() throws RuntimeException { System.out.println("Making zip archive of files in " + getCurrentDirectory() + ", putting it in " + getArchiveDirectory() + "."); File current = new File(getCurrentDirectory()); if (!current.exists() || !current.isDirectory()) { throw new IllegalArgumentException("There is no " + current.getAbsolutePath() + " directory. " + "\nThis is where the serialized classes should be. " + "Please run serializeCurrentDirectory() first."); } File archive = new File(getArchiveDirectory()); if (archive.exists() && !archive.isDirectory()) { throw new IllegalArgumentException( "Output directory " + archive.getAbsolutePath() + " is not a directory."); } if (!archive.exists()) { archive.mkdirs(); } String[] filenames = current.list(); // Create a buffer for reading the files byte[] buf = new byte[1024]; try { String version = Version.currentRepositoryVersion().toString(); // Create the ZIP file String outFilename = "serializedclasses-" + version + ".zip"; File _file = new File(getArchiveDirectory(), outFilename); FileOutputStream fileOut = new FileOutputStream(_file); ZipOutputStream out = new ZipOutputStream(fileOut); // Compress the files for (String filename : filenames) { File file = new File(current, filename); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(filename); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("Finished writing zip file " + outFilename + "."); } catch (IOException e) { throw new RuntimeException("There was an I/O error associated with " + "the process of zipping up files in " + getCurrentDirectory() + ".", e); } }
From source file:eu.europa.esig.dss.asic.signature.ASiCService.java
private ZipEntry getZipEntryMimeType(final byte[] mimeTypeBytes) { final ZipEntry entryMimetype = new ZipEntry(ZIP_ENTRY_MIMETYPE); entryMimetype.setMethod(ZipEntry.STORED); entryMimetype.setSize(mimeTypeBytes.length); entryMimetype.setCompressedSize(mimeTypeBytes.length); final CRC32 crc = new CRC32(); crc.update(mimeTypeBytes);//from ww w . j av a 2 s.co m entryMimetype.setCrc(crc.getValue()); return entryMimetype; }
From source file:de.mpg.escidoc.services.exportmanager.Export.java
private void addDescriptionEnrty(byte[] exportOut, String exportFormat, OutputStream aos) throws IOException, ExportManagerException { if (exportOut == null || exportOut.length == 0) return;/*from w w w. jav a 2s . c o m*/ Utils.checkCondition(aos == null, "Archive OutputStream is null"); Utils.checkCondition(!Utils.checkVal(exportFormat), "Empty export format"); String entryName = "eSciDoc_export." + exportFormat.toLowerCase(); BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(exportOut)); if (aos instanceof ZipOutputStream) { ZipEntry ze = new ZipEntry(entryName); ze.setSize(exportOut.length); ((ZipOutputStream) aos).putNextEntry(ze); writeFromStreamToStream(bis, aos); ((ZipOutputStream) aos).closeEntry(); bis.close(); } else if (aos instanceof TarOutputStream) { TarEntry te = new TarEntry(entryName); te.setSize(exportOut.length); ((TarOutputStream) aos).putNextEntry(te); writeFromStreamToStream(bis, aos); ((TarOutputStream) aos).closeEntry(); bis.close(); } else { throw new ExportManagerException("Wrong archive OutputStream: " + aos); } }
From source file:de.mpg.mpdl.inge.exportmanager.Export.java
/** * Walk around the itemList XML, fetch all files from components via URIs and put them into the * archive {@link OutputStream} aos// www . j a v a2s.c om * * @param aos - array {@link OutputStream} * @param itemList - XML with the files to be fetched, see NS: * http://www.escidoc.de/schemas/components/0.7 * @throws ExportManagerException */ private void fetchComponentsDo(OutputStream aos, String itemList) throws ExportManagerException { Document doc = parseDocument(itemList); NodeIterator ni = getFilteredNodes(new ComponentNodeFilter(), doc); // login only once String userHandle; try { userHandle = AdminHelper.loginUser(USER_ID, PASSWORD); } catch (Exception e) { throw new ExportManagerException("Cannot login", e); } String fileName; Node n; while ((n = ni.nextNode()) != null) { Element componentElement = (Element) n; NodeList nl = componentElement.getElementsByTagNameNS(COMPONENTS_NS, "content"); Element contentElement = (Element) nl.item(0); if (contentElement == null) { throw new ExportManagerException( "Wrong item XML: {" + COMPONENTS_NS + "}component element doesn't contain content element. " + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } String href = contentElement.getAttributeNS(XLINK_NS, "href"); String storageStatus = contentElement.getAttribute("storage"); // get file name if ("internal-managed".equals(storageStatus)) { NodeIterator nif = ((DocumentTraversal) doc).createNodeIterator(componentElement, NodeFilter.SHOW_ELEMENT, new FileNameNodeFilter(), true); Node nf; if ((nf = nif.nextNode()) != null) { fileName = ((Element) nf).getTextContent(); // names of files for Matcher m = Pattern.compile("^([\\w.]+?)(\\s+|$)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL) .matcher(fileName); m.find(); fileName = m.group(1); } else { throw new ExportManagerException("Missed file property: {" + COMPONENTS_NS + "}component element doesn't contain file-name element (md-records/md-record/file:file/dc:title). " + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } } // TODO: the external-managed will be processed later else { throw new ExportManagerException("Missed internal-managed file in {" + COMPONENTS_NS + "}component: components/component/content[@storage=\"internal-managed\"]" + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } logger.info("link to the content: " + href); logger.info("storage status: " + storageStatus); logger.info("fileName: " + fileName); // get file via URI String url; try { url = PropertyReader.getFrameworkUrl() + href; } catch (Exception e) { throw new ExportManagerException("Cannot get framework url", e); } logger.info("url=" + url); GetMethod method = new GetMethod(url); method.setFollowRedirects(false); method.setRequestHeader("Cookie", "escidocCookie=" + userHandle); // Execute the method with HttpClient. HttpClient client = new HttpClient(); try { ProxyHelper.executeMethod(client, method); } catch (Exception e) { throw new ExportManagerException("Cannot execute HttpMethod", e); } int status = method.getStatusCode(); logger.info("Status=" + status); if (status != 200) fileName += ".error" + status; byte[] responseBody; try { responseBody = method.getResponseBody(); } catch (Exception e) { throw new ExportManagerException("Cannot get Response Body", e); } InputStream bis = new BufferedInputStream(new ByteArrayInputStream(responseBody)); if (aos instanceof ZipOutputStream) { ZipEntry ze = new ZipEntry(fileName); ze.setSize(responseBody.length); try { ((ZipOutputStream) aos).putNextEntry(ze); writeFromStreamToStream(bis, aos); ((ZipOutputStream) aos).closeEntry(); } catch (Exception e) { throw new ExportManagerException("zip2stream generation problem", e); } } else if (aos instanceof TarOutputStream) { TarEntry te = new TarEntry(fileName); te.setSize(responseBody.length); try { ((TarOutputStream) aos).putNextEntry(te); writeFromStreamToStream(bis, aos); ((TarOutputStream) aos).closeEntry(); } catch (Exception e) { throw new ExportManagerException("tar2stream generation problem", e); } } else { throw new ExportManagerException("Unsupported archive output stream: " + aos.getClass()); } try { bis.close(); } catch (Exception e) { throw new ExportManagerException("Cannot close InputStream", e); } } }
From source file:com.zimbra.cs.zimlet.ZimletUtil.java
private static void addZipEntry(ZipOutputStream out, File file, String path) throws IOException { String name = (path == null) ? file.getName() : path + "/" + file.getName(); if (file.isDirectory()) { for (File f : file.listFiles()) { addZipEntry(out, f, name);/*from w w w. java2s . c om*/ } return; } ZipEntry entry = new ZipEntry(name); entry.setMethod(ZipEntry.STORED); entry.setSize(file.length()); entry.setCompressedSize(file.length()); entry.setCrc(computeCRC32(file)); out.putNextEntry(entry); ByteUtil.copy(new FileInputStream(file), true, out, false); out.closeEntry(); }
From source file:de.mpg.escidoc.services.exportmanager.Export.java
/** * Walk around the itemList XML, fetch all files from components via URIs * and put them into the archive {@link OutputStream} aos * /*from w w w. ja v a 2 s. c om*/ * @param aos * - array {@link OutputStream} * @param itemList * - XML with the files to be fetched, see NS: * http://www.escidoc.de/schemas/components/0.7 * @throws ExportManagerException */ private void fetchComponentsDo(OutputStream aos, String itemList) throws ExportManagerException { Document doc = parseDocument(itemList); NodeIterator ni = getFilteredNodes(new ComponentNodeFilter(), doc); // login only once String userHandle; try { userHandle = AdminHelper.loginUser(USER_ID, PASSWORD); } catch (Exception e) { throw new ExportManagerException("Cannot login", e); } String fileName; Node n; while ((n = ni.nextNode()) != null) { Element componentElement = (Element) n; NodeList nl = componentElement.getElementsByTagNameNS(COMPONENTS_NS, "content"); Element contentElement = (Element) nl.item(0); if (contentElement == null) { throw new ExportManagerException( "Wrong item XML: {" + COMPONENTS_NS + "}component element doesn't contain content element. " + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } String href = contentElement.getAttributeNS(XLINK_NS, "href"); String storageStatus = contentElement.getAttribute("storage"); // get file name if ("internal-managed".equals(storageStatus)) { NodeIterator nif = ((DocumentTraversal) doc).createNodeIterator(componentElement, NodeFilter.SHOW_ELEMENT, new FileNameNodeFilter(), true); Node nf; if ((nf = nif.nextNode()) != null) { fileName = ((Element) nf).getTextContent(); // names of files for Matcher m = Pattern.compile("^([\\w.]+?)(\\s+|$)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL) .matcher(fileName); m.find(); fileName = m.group(1); } else { throw new ExportManagerException("Missed file property: {" + COMPONENTS_NS + "}component element doesn't contain file-name element (md-records/md-record/file:file/dc:title). " + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } } // TODO: the external-managed will be processed later else { throw new ExportManagerException("Missed internal-managed file in {" + COMPONENTS_NS + "}component: components/component/content[@storage=\"internal-managed\"]" + "Component id: " + componentElement.getAttributeNS(XLINK_NS, "href")); } logger.info("link to the content: " + href); logger.info("storage status: " + storageStatus); logger.info("fileName: " + fileName); // get file via URI String url; try { url = ServiceLocator.getFrameworkUrl() + href; } catch (Exception e) { throw new ExportManagerException("Cannot get framework url", e); } logger.info("url=" + url); GetMethod method = new GetMethod(url); method.setFollowRedirects(false); method.setRequestHeader("Cookie", "escidocCookie=" + userHandle); // Execute the method with HttpClient. HttpClient client = new HttpClient(); try { ProxyHelper.executeMethod(client, method); } catch (Exception e) { throw new ExportManagerException("Cannot execute HttpMethod", e); } int status = method.getStatusCode(); logger.info("Status=" + status); if (status != 200) fileName += ".error" + status; byte[] responseBody; try { responseBody = method.getResponseBody(); } catch (Exception e) { throw new ExportManagerException("Cannot get Response Body", e); } InputStream bis = new BufferedInputStream(new ByteArrayInputStream(responseBody)); if (aos instanceof ZipOutputStream) { ZipEntry ze = new ZipEntry(fileName); ze.setSize(responseBody.length); try { ((ZipOutputStream) aos).putNextEntry(ze); writeFromStreamToStream(bis, aos); ((ZipOutputStream) aos).closeEntry(); } catch (Exception e) { throw new ExportManagerException("zip2stream generation problem", e); } } else if (aos instanceof TarOutputStream) { TarEntry te = new TarEntry(fileName); te.setSize(responseBody.length); try { ((TarOutputStream) aos).putNextEntry(te); writeFromStreamToStream(bis, aos); ((TarOutputStream) aos).closeEntry(); } catch (Exception e) { throw new ExportManagerException("tar2stream generation problem", e); } } else { throw new ExportManagerException("Unsupported archive output stream: " + aos.getClass()); } try { bis.close(); } catch (Exception e) { throw new ExportManagerException("Cannot close InputStream", e); } } }