List of usage examples for java.util.zip ZipEntry setCrc
public void setCrc(long crc)
From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java
public static File buildArchive(File archive, File... files) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream(archive); ZipOutputStream zos = new ZipOutputStream(fos); int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); for (File file : files) { if (!file.exists()) { System.err.println("Skipping: " + file); continue; }// w ww . j av a 2 s .c om BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); crc.reset(); while ((bytesRead = bis.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } bis.close(); // Reset to beginning of input stream bis = new BufferedInputStream(new FileInputStream(file)); String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file); ZipEntry entry = new ZipEntry(entryPath); entry.setMethod(ZipEntry.STORED); entry.setCompressedSize(file.length()); entry.setSize(file.length()); entry.setCrc(crc.getValue()); zos.putNextEntry(entry); while ((bytesRead = bis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { IOUtils.closeQuietly(bis); } } IOUtils.closeQuietly(zos); return archive; }
From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java
private static void storeOpenDocumentSpreadsheetFile(File file, InputStream source) throws Exception { try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { //addResourceFile("mimetype", "/resource/ods/mimetype", out); ZipEntry ze = new ZipEntry("mimetype"); String mime = "application/vnd.oasis.opendocument.spreadsheet"; ze.setMethod(ZipEntry.STORED); ze.setSize(mime.length());/*w ww. j a v a 2s .c o m*/ CRC32 crc = new CRC32(); crc.update(mime.getBytes()); ze.setCrc(crc.getValue()); out.putNextEntry(ze); for (int i = 0; i < mime.length(); i++) { out.write(mime.charAt(i)); } out.closeEntry(); ZipEntry zipEntry = new ZipEntry("content.xml"); //zipEntry.setMethod(ZipEntry.DEFLATED); out.putNextEntry(zipEntry); int c; while ((c = source.read()) >= 0) { out.write(c); } out.closeEntry(); // Add manifest (required for OOo 2.0) and "meta.xml": These are in the // resource/ods directory, and are copied verbatim into the zip file. OpenDocumentSpreadsheetCreator.addResourceFile("meta.xml", "/resource/ods/meta.xml", out); OpenDocumentSpreadsheetCreator.addResourceFile("META-INF/manifest.xml", "/resource/ods/manifest.xml", out); } }
From source file:JarUtils.java
/** * This recursive method writes all matching files and directories to * the jar output stream./*from w ww . j a v a 2s . co m*/ */ private static void jar(File src, String prefix, JarInfo info) throws IOException { JarOutputStream jout = info.out; if (src.isDirectory()) { // create / init the zip entry prefix = prefix + src.getName() + "/"; ZipEntry entry = new ZipEntry(prefix); entry.setTime(src.lastModified()); entry.setMethod(JarOutputStream.STORED); entry.setSize(0L); entry.setCrc(0L); jout.putNextEntry(entry); jout.closeEntry(); // process the sub-directories File[] files = src.listFiles(info.filter); for (int i = 0; i < files.length; i++) { jar(files[i], prefix, info); } } else if (src.isFile()) { // get the required info objects byte[] buffer = info.buffer; // create / init the zip entry ZipEntry entry = new ZipEntry(prefix + src.getName()); entry.setTime(src.lastModified()); jout.putNextEntry(entry); // dump the file FileInputStream in = new FileInputStream(src); int len; while ((len = in.read(buffer, 0, buffer.length)) != -1) { jout.write(buffer, 0, len); } in.close(); jout.closeEntry(); } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
/** * @param basePath/*from ww w. j a v a 2 s .co m*/ * @param zipPath * @param filePaths * @throws java.io.IOException */ public static void zip(File basePath, File zipPath, Map<String, String> filePaths) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = null; FileOutputStream dest = null; try { dest = new FileOutputStream(zipPath); out = new ZipOutputStream(new BufferedOutputStream(dest)); //out.setMethod(ZipOutputStream.DEFLATED); byte data[] = new byte[BUFFER]; for (Map.Entry<String, String> file : filePaths.entrySet()) { String filename = file.getKey(); String filePath = file.getValue(); System.out.println("Adding: " + basePath.getPath() + filePath + " => " + filename); File f = new File(basePath, filePath); if (f.exists()) { FileInputStream fi = new FileInputStream(f); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(filename); entry.setCrc(FileUtils.checksumCRC32(new File(basePath, filePath))); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } } } finally { if (origin != null) origin.close(); if (out != null) out.close(); if (dest != null) dest.close(); } }
From source file:eionet.gdem.utils.ZipUtil.java
/** * * @param f// www. jav a 2 s . c o m * - File that will be zipped * @param outZip * - ZipOutputStream represents the zip file, where the files will be placed * @param sourceDir * - root directory, where the zipping started * @param doCompress * - don't comress the file, if doCompress=true * @throws IOException If an error occurs. */ public static void zipFile(File f, ZipOutputStream outZip, String sourceDir, boolean doCompress) throws IOException { // Read the source file into byte array byte[] fileBytes = Utils.getBytesFromFile(f); // create a new zip entry String strAbsPath = f.getPath(); String strZipEntryName = strAbsPath.substring(sourceDir.length() + 1, strAbsPath.length()); strZipEntryName = Utils.Replace(strZipEntryName, File.separator, "/"); ZipEntry anEntry = new ZipEntry(strZipEntryName); // Don't compress the file, if not needed if (!doCompress) { // ZipEntry can't calculate crc size automatically, if we use STORED method. anEntry.setMethod(ZipEntry.STORED); anEntry.setSize(fileBytes.length); CRC32 crc321 = new CRC32(); crc321.update(fileBytes); anEntry.setCrc(crc321.getValue()); } // place the zip entry in the ZipOutputStream object outZip.putNextEntry(anEntry); // now write the content of the file to the ZipOutputStream outZip.write(fileBytes); outZip.flush(); // Close the current entry outZip.closeEntry(); }
From source file:brut.directory.ZipUtils.java
private static void processFolder(final File folder, final ZipOutputStream zipOutputStream, final int prefixLength) throws BrutException, IOException { for (final File file : folder.listFiles()) { if (file.isFile()) { final String cleanedPath = BrutIO.sanitizeUnknownFile(folder, file.getPath().substring(prefixLength)); final ZipEntry zipEntry = new ZipEntry(BrutIO.normalizePath(cleanedPath)); // aapt binary by default takes in parameters via -0 arsc to list extensions that shouldn't be // compressed. We will replicate that behavior final String extension = FilenameUtils.getExtension(file.getAbsolutePath()); if (mDoNotCompress != null && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) { zipEntry.setMethod(ZipEntry.STORED); zipEntry.setSize(file.length()); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = BrutIO.calculateCrc(unknownFile); zipEntry.setCrc(crc.getValue()); unknownFile.close();//from w ww . ja v a 2 s .c o m } else { zipEntry.setMethod(ZipEntry.DEFLATED); } zipOutputStream.putNextEntry(zipEntry); try (FileInputStream inputStream = new FileInputStream(file)) { IOUtils.copy(inputStream, zipOutputStream); } zipOutputStream.closeEntry(); } else if (file.isDirectory()) { processFolder(file, zipOutputStream, prefixLength); } } }
From source file:it.cnr.icar.eric.common.Utility.java
public static ZipOutputStream createZipOutputStream(String baseDir, String[] relativeFilePaths, OutputStream os) throws FileNotFoundException, IOException { if (baseDir.startsWith("file:/")) { baseDir = baseDir.substring(5);/*from w ww . ja va2 s . c o m*/ } ZipOutputStream zipoutputstream = new ZipOutputStream(os); zipoutputstream.setMethod(ZipOutputStream.STORED); for (int i = 0; i < relativeFilePaths.length; i++) { File file = new File(baseDir + FILE_SEPARATOR + relativeFilePaths[i]); byte[] buffer = new byte[1000]; int n; FileInputStream fis; // Calculate the CRC-32 value. This isn't strictly necessary // for deflated entries, but it doesn't hurt. CRC32 crc32 = new CRC32(); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { crc32.update(buffer, 0, n); } fis.close(); // Create a zip entry. ZipEntry zipEntry = new ZipEntry(relativeFilePaths[i]); zipEntry.setSize(file.length()); zipEntry.setTime(file.lastModified()); zipEntry.setCrc(crc32.getValue()); // Add the zip entry and associated data. zipoutputstream.putNextEntry(zipEntry); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { zipoutputstream.write(buffer, 0, n); } fis.close(); zipoutputstream.closeEntry(); } return zipoutputstream; }
From source file:net.librec.util.FileUtil.java
/** * Zip a given folder//from ww w . j ava2 s . c o m * * @param dirPath a given folder: must be all files (not sub-folders) * @param filePath zipped file * @throws Exception if error occurs */ public static void zipFolder(String dirPath, String filePath) throws Exception { File outFile = new File(filePath); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFile)); int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); for (File file : listFiles(dirPath)) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); crc.reset(); while ((bytesRead = bis.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } bis.close(); // Reset to beginning of input stream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(file.getName()); entry.setMethod(ZipEntry.STORED); entry.setCompressedSize(file.length()); entry.setSize(file.length()); entry.setCrc(crc.getValue()); zos.putNextEntry(entry); while ((bytesRead = bis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } bis.close(); } zos.close(); LOG.debug("A zip-file is created to: " + outFile.getPath()); }
From source file:Main.java
public void doZip(String filename, String zipfilename) throws Exception { byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(filename); fis.read(buf, 0, buf.length);/*ww w. j a va 2s .c o m*/ CRC32 crc = new CRC32(); ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename)); s.setLevel(6); ZipEntry entry = new ZipEntry(filename); entry.setSize((long) buf.length); crc.reset(); crc.update(buf); entry.setCrc(crc.getValue()); s.putNextEntry(entry); s.write(buf, 0, buf.length); s.finish(); s.close(); }
From source file:Main.java
public void doZip(String filename, String zipfilename) throws Exception { byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(filename); fis.read(buf, 0, buf.length);//from ww w .ja va 2 s . c om CRC32 crc = new CRC32(); ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename)); s.setLevel(6); ZipEntry entry = new ZipEntry(filename); entry.setSize((long) buf.length); entry.setMethod(ZipEntry.DEFLATED); crc.reset(); crc.update(buf); entry.setCrc(crc.getValue()); s.putNextEntry(entry); s.write(buf, 0, buf.length); s.finish(); s.close(); }