Java Code Examples for org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream#finish()

The following examples show how to use org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream#finish() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompressExample.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
public static void makeZip() throws IOException, ArchiveException{
		File f1 = new File("D:/compresstest.txt");
        File f2 = new File("D:/test1.xml");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        //ArchiveOutputStream ostemp = new ArchiveStreamFactory().createArchiveOutputStream("zip", baos);
        ZipArchiveOutputStream ostemp = new ZipArchiveOutputStream(baos);
        ostemp.setEncoding("GBK");
        ostemp.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
        IOUtils.copy(new FileInputStream(f1), ostemp);
        ostemp.closeArchiveEntry();
        ostemp.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
        IOUtils.copy(new FileInputStream(f2), ostemp);
        ostemp.closeArchiveEntry();
        ostemp.finish();
        ostemp.close();

//      final OutputStream out = new FileOutputStream("D:/testcompress.zip");
        final OutputStream out = new FileOutputStream("D:/中文名字.zip");
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);
        os.putArchiveEntry(new ZipArchiveEntry("打包.zip"));
        baos.writeTo(os);
        os.closeArchiveEntry();
        baos.close();
        os.finish();
        os.close();
	}
 
Example 2
Source File: ArchiveUtils.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create ZIP archive from file
 */
public static void createZip(File path, OutputStream os) throws IOException {
	log.debug("createZip({}, {})", new Object[]{path, os});

	if (path.exists() && path.canRead()) {
		ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
		zaos.setComment("Generated by OpenKM");
		zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
		zaos.setUseLanguageEncodingFlag(true);
		zaos.setFallbackToUTF8(true);
		zaos.setEncoding("UTF-8");

		log.debug("FILE {}", path);
		ZipArchiveEntry zae = new ZipArchiveEntry(path.getName());
		zaos.putArchiveEntry(zae);
		FileInputStream fis = new FileInputStream(path);
		IOUtils.copy(fis, zaos);
		fis.close();
		zaos.closeArchiveEntry();

		zaos.flush();
		zaos.finish();
		zaos.close();
	} else {
		throw new IOException("Can't access " + path);
	}

	log.debug("createZip: void");
}
 
Example 3
Source File: ArchiveUtils.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Recursively create ZIP archive from directory
 */
public static void createZip(File path, String root, OutputStream os) throws IOException {
	log.debug("createZip({}, {}, {})", new Object[]{path, root, os});

	if (path.exists() && path.canRead()) {
		ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
		zaos.setComment("Generated by OpenKM");
		zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
		zaos.setUseLanguageEncodingFlag(true);
		zaos.setFallbackToUTF8(true);
		zaos.setEncoding("UTF-8");

		// Prevents java.util.zip.ZipException: ZIP file must have at least one entry
		ZipArchiveEntry zae = new ZipArchiveEntry(root + "/");
		zaos.putArchiveEntry(zae);
		zaos.closeArchiveEntry();

		createZipHelper(path, zaos, root);

		zaos.flush();
		zaos.finish();
		zaos.close();
	} else {
		throw new IOException("Can't access " + path);
	}

	log.debug("createZip: void");
}
 
Example 4
Source File: MCRZipServlet.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void disposeContainer(ZipArchiveOutputStream container) throws IOException {
    container.finish();
}