List of usage examples for java.util.zip ZipEntry setSize
public void setSize(long size)
From source file:Main.java
private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException { if (srcFile.isDirectory()) { File subfile[] = srcFile.listFiles(); for (int i = 0; i < subfile.length; i++) { compressDir(subfile[i], out, destPath); }/* www.jav a 2 s. c o m*/ } else { InputStream in = new FileInputStream(srcFile); String name = srcFile.getAbsolutePath().replace(destPath, ""); if (name.startsWith("\\")) name = name.substring(1); ZipEntry entry = new ZipEntry(name); entry.setSize(srcFile.length()); entry.setTime(srcFile.lastModified()); out.putNextEntry(entry); int len = -1; byte buf[] = new byte[1024]; while ((len = in.read(buf, 0, 1024)) != -1) out.write(buf, 0, len); out.closeEntry(); in.close(); } }
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 w w . ja va 2 s .c o m*/ 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()); CRC32 crc = new CRC32(); crc.update(mime.getBytes());//from w w w. ja v a 2 s .c o m 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:com.eviware.soapui.testondemand.TestOnDemandCaller.java
private static byte[] zipBytes(String filename, byte[] dataToBeZiped) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zipedOutputStream = new ZipOutputStream(outputStream); ZipEntry entry = new ZipEntry(filename); entry.setSize(dataToBeZiped.length); try {/* www . j av a 2 s . c o m*/ zipedOutputStream.putNextEntry(entry); zipedOutputStream.write(dataToBeZiped); } finally { zipedOutputStream.closeEntry(); zipedOutputStream.close(); } return outputStream.toByteArray(); }
From source file:com.algomedica.service.LicenseManagerServiceImpl.java
public static byte[] zipBytes(String filename, byte[] input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); ZipEntry entry = new ZipEntry(filename); entry.setSize(input.length); zos.putNextEntry(entry);// w w w .java2 s. c om zos.write(input); zos.closeEntry(); zos.close(); return baos.toByteArray(); }
From source file:JarUtils.java
/** * This recursive method writes all matching files and directories to * the jar output stream./* w ww .j a v a 2 s .c o 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:eionet.gdem.utils.ZipUtil.java
/** * * @param f/*from w w w. j ava 2 s . co 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 w w.j a 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);/* w w w. jav a 2 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:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * ?zip?solib?/* w ww. j a va 2s. c o m*/ * * @param output * @param srcDir * @throws Exception */ public static void addFileAndDirectoryToZip(File output, File srcDir) throws Exception { if (output.isDirectory()) { throw new IOException("This is a directory!"); } if (!output.getParentFile().exists()) { output.getParentFile().mkdirs(); } if (!output.exists()) { output.createNewFile(); } List fileList = getSubFiles(srcDir); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); }