/** * Closes this output stream and releases any system resources * associated with the stream. * * @throws IOException if an I/O error occurs. * @throws Zip64RequiredException if the archive's size exceeds 4 * GByte or there are more than 65535 entries inside the archive * and {@link #setUseZip64} is {@link Zip64Mode#Never}. */ @Override public void close() throws IOException { try { if (!finished) { finish(); } } finally { destroy(); } }
zos.finish(); zipEntrySource.close();
zos.finish(); } catch (OpenXML4JRuntimeException e) {
outputStream.finish(); outputStream.close();
/** * Closes this output stream and releases any system resources * associated with the stream. * * @throws IOException if an I/O error occurs. * @throws Zip64RequiredException if the archive's size exceeds 4 * GByte or there are more than 65535 entries inside the archive * and {@link #setUseZip64} is {@link Zip64Mode#Never}. */ @Override public void close() throws IOException { if (!finished) { finish(); } destroy(); }
public static void createZip(String directoryPath, String zipPath) throws IOException { FileOutputStream fOut = null; BufferedOutputStream bOut = null; ZipArchiveOutputStream tOut = null; try { fOut = new FileOutputStream(new File(zipPath)); bOut = new BufferedOutputStream(fOut); tOut = new ZipArchiveOutputStream(bOut); addFileToZip(tOut, directoryPath, ""); } finally { tOut.finish(); tOut.close(); bOut.close(); fOut.close(); } }
/** * Creates a zip file at the specified path with the contents of the specified directory. * NB: * * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip * @throws IOException If anything goes wrong */ public static void createZip(String directoryPath, String zipPath) throws IOException { FileOutputStream fOut = null; BufferedOutputStream bOut = null; ZipArchiveOutputStream tOut = null; try { fOut = new FileOutputStream(new File(zipPath)); bOut = new BufferedOutputStream(fOut); tOut = new ZipArchiveOutputStream(bOut); addFileToZip(tOut, directoryPath, ""); } finally { tOut.finish(); tOut.close(); bOut.close(); fOut.close(); } }
FileUtils.writeStringToFile(new File(hashPath), hash); } finally { zOut.finish(); zOut.close(); bOut.close();
zipArchiveOutputStream.finish(); } catch (Exception e) { throw new RuntimeException(e);
/** * 把文件压缩成zip格式 * @param files 需要压缩的文件集合 * @param zipFilePath 压缩后的zip文件路径 ,如"D:/test/aa.zip"; */ public static void zip(File[] files, String zipFilePath) { if (files != null && files.length > 0) { if (isZip(zipFilePath)) { try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(new File(zipFilePath))) { //使用Zip64扩展 zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded); // 将每个文件用ZipArchiveEntry封装,再用ZipArchiveOutputStream写到压缩文件中 for (File file : files) { if (file.exists()) { ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName()); zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry); if (file.isDirectory()) { continue; } IOUtils.copy(new FileInputStream(file), zipArchiveOutputStream); zipArchiveOutputStream.closeArchiveEntry(); } } zipArchiveOutputStream.finish(); } catch (Exception e) { throw new RuntimeException(e); } } } }
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.util.Arrays; public class Test { public static void main(String[] args) throws Exception { ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(System.out); byte[] data = new byte[1024*1024]; for(byte i=49; i<120; i++) { Arrays.fill(data, i); ArchiveEntry file1 = new ZipArchiveEntry("file" + i + ".txt"); zipOut.putArchiveEntry(file1); zipOut.write(data); zipOut.closeArchiveEntry(); } zipOut.finish(); zipOut.close(); } }
zos.finish(); zipEntrySource.close();
zos.finish(); } catch (OpenXML4JRuntimeException e) {
@Override public void decrypt(XKey key, InputStream in, OutputStream out) throws IOException { ZipArchiveInputStream zis = null; ZipArchiveOutputStream zos = null; try { zis = new ZipArchiveInputStream(in); zos = new ZipArchiveOutputStream(out); zos.setLevel(level); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); ZipArchiveEntry entry; while ((entry = zis.getNextZipEntry()) != null) { if (entry.isDirectory()) { continue; } zos.putArchiveEntry(new ZipArchiveEntry(entry.getName())); XDecryptor decryptor = filtrate(entry) ? this : xNopDecryptor; try (OutputStream eos = decryptor.decrypt(key, nos)) { XKit.transfer(zis, eos); } zos.closeArchiveEntry(); } zos.finish(); } finally { XKit.close(zis); XKit.close(zos); } }
@Override public void encrypt(XKey key, InputStream in, OutputStream out) throws IOException { ZipArchiveInputStream zis = null; ZipArchiveOutputStream zos = null; try { zis = new ZipArchiveInputStream(in); zos = new ZipArchiveOutputStream(out); zos.setLevel(level); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); ZipArchiveEntry entry; while ((entry = zis.getNextZipEntry()) != null) { if (entry.isDirectory()) { continue; } zos.putArchiveEntry(new ZipArchiveEntry(entry.getName())); XEncryptor encryptor = filtrate(entry) ? this : xNopEncryptor; try (OutputStream eos = encryptor.encrypt(key, nos)) { XKit.transfer(zis, eos); } zos.closeArchiveEntry(); } zos.finish(); } finally { XKit.close(zis); XKit.close(zos); } } }