Search in sources :

Example 41 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project SmartAndroidSource by jaychou2012.

the class ZipUtil method packEntry.

/* Compressing single entries to ZIP files. */
/**
	 * Compresses the given file into a ZIP file with single entry.
	 * 
	 * @param file
	 *            file to be compressed.
	 * @return ZIP file created.
	 */
public static byte[] packEntry(File file) {
    // log.trace("Compressing '{}' into a ZIP file with single entry.",
    // file);
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    try {
        ZipOutputStream out = new ZipOutputStream(result);
        ZipEntry entry = new ZipEntry(file.getName());
        entry.setTime(file.lastModified());
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        try {
            ZipEntryUtil.addEntry(entry, in, out);
        } finally {
            IOUtils.closeQuietly(in);
        }
        out.close();
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    }
    return result.toByteArray();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 42 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project SmartAndroidSource by jaychou2012.

the class ZipUtil method replaceEntries.

/**
	 * Copies an existing ZIP file and replaces the given entries in it.
	 * 
	 * @param zip
	 *            an existing ZIP file (only read).
	 * @param entries
	 *            new ZIP entries to be replaced with.
	 * @param destZip
	 *            new ZIP file created.
	 * @return <code>true</code> if at least one entry was replaced.
	 */
public static boolean replaceEntries(File zip, ZipEntrySource[] entries, File destZip) {
    // if (log.isDebugEnabled()) {
    // log.debug("Copying '" + zip + "' to '" + destZip
    // + "' and replacing entries " + Arrays.asList(entries) + ".");
    // }
    final Map<String, ZipEntrySource> entryByPath = entriesByPath(entries);
    final int entryCount = entryByPath.size();
    try {
        final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZip)));
        try {
            final Set<String> names = new HashSet<String>();
            iterate(zip, new ZipEntryCallback() {

                public void process(InputStream in, ZipEntry zipEntry) throws IOException {
                    if (names.add(zipEntry.getName())) {
                        ZipEntrySource entry = (ZipEntrySource) entryByPath.remove(zipEntry.getName());
                        if (entry != null) {
                            addEntry(entry, out);
                        } else {
                            ZipEntryUtil.copyEntry(zipEntry, in, out);
                        }
                    }
                // else if (log.isDebugEnabled()) {
                // log.debug("Duplicate entry: {}", zipEntry.getName());
                // }
                }
            });
        } finally {
            IOUtils.closeQuietly(out);
        }
    } catch (IOException e) {
        ZipExceptionUtil.rethrow(e);
    }
    return entryByPath.size() < entryCount;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet)

Example 43 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project Lazy by l123456789jy.

the class ZipUtil method zipFiles.

/**
     * 批量压缩文件(夹)
     *
     * @param resFileList 要压缩的文件(夹)列表
     * @param zipFile 生成的压缩文件
     * @param zipListener     zipListener
     */
public static void zipFiles(Collection<File> resFileList, File zipFile, ZipListener zipListener) {
    ZipOutputStream zipout = null;
    try {
        zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            if (stopZipFlag) {
                break;
            }
            zipFile(resFile, zipout, "", zipListener);
        }
        zipout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 44 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project neo4j by neo4j.

the class ReadablesTest method compressWithZip.

// TODO test for failing reading a ZIP archive with multiple files in
private File compressWithZip(String text, String... otherEntries) throws IOException {
    File file = directory.file("compressed");
    try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) {
        for (String otherEntry : otherEntries) {
            out.putNextEntry(new ZipEntry(otherEntry));
        }
        out.putNextEntry(new ZipEntry("file"));
        out.write(text.getBytes());
    }
    return file;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File)

Example 45 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project neo4j by neo4j.

the class PartitionedIndexStorage method cleanupFolder.

private void cleanupFolder(File folder, boolean archiveFailed) throws IOException {
    List<File> partitionFolders = listFolders(folder);
    if (!partitionFolders.isEmpty()) {
        try (ZipOutputStream zip = archiveFile(folder, archiveFailed)) {
            byte[] buffer = null;
            if (zip != null) {
                buffer = new byte[4 * 1024];
            }
            for (File partitionFolder : partitionFolders) {
                cleanupLuceneDirectory(partitionFolder, zip, buffer);
            }
        }
    }
    fileSystem.deleteRecursively(folder);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) File(java.io.File)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)463 ZipEntry (java.util.zip.ZipEntry)308 FileOutputStream (java.io.FileOutputStream)216 File (java.io.File)181 IOException (java.io.IOException)118 ZipFile (java.util.zip.ZipFile)104 BufferedOutputStream (java.io.BufferedOutputStream)90 ByteArrayOutputStream (java.io.ByteArrayOutputStream)83 FileInputStream (java.io.FileInputStream)82 Test (org.junit.Test)68 InputStream (java.io.InputStream)61 ZipInputStream (java.util.zip.ZipInputStream)44 ByteArrayInputStream (java.io.ByteArrayInputStream)39 BufferedInputStream (java.io.BufferedInputStream)28 OutputStream (java.io.OutputStream)26 Path (java.nio.file.Path)22 ArrayList (java.util.ArrayList)19 FileNotFoundException (java.io.FileNotFoundException)18 HashMap (java.util.HashMap)17 CRC32 (java.util.zip.CRC32)13