List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setEncoding
public void setEncoding(final String encoding)
From source file:org.apache.ant.compress.util.ZipStreamFactory.java
/** * @param stream the stream to write to, should be buffered * @param encoding the encoding of the entry names *///from ww w .j ava 2 s . co m public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException { ZipArchiveOutputStream o = new ZipArchiveOutputStream(stream); o.setEncoding(encoding); return o; }
From source file:org.apache.ant.compress.util.ZipStreamFactory.java
/** * @param file the file to write to/*from w ww. j a v a 2 s.c om*/ * @param encoding the encoding of the entry names */ public ArchiveOutputStream getArchiveOutputStream(File file, String encoding) throws IOException { ZipArchiveOutputStream o = new ZipArchiveOutputStream(file); o.setEncoding(encoding); return o; }
From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java
@Override public ArchiveOutputStream createArchiveOutputStream(final String archiverName, final OutputStream out, final String actualEncoding) throws ArchiveException { if (archiverName == null) { throw new IllegalArgumentException("Archivername must not be null."); }/*w w w .j a v a2 s .co m*/ if (out == null) { throw new IllegalArgumentException("OutputStream must not be null."); } if (AR.equalsIgnoreCase(archiverName)) { return new ArArchiveOutputStream(out); } if (ZIP.equalsIgnoreCase(archiverName)) { final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(out); if (actualEncoding != null) { zip.setEncoding(actualEncoding); } return zip; } if (TAR.equalsIgnoreCase(archiverName)) { if (actualEncoding != null) { return new TarArchiveOutputStream(out, actualEncoding); } return new TarArchiveOutputStream(out); } if (JAR.equalsIgnoreCase(archiverName)) { if (actualEncoding != null) { return new JarArchiveOutputStream(out, actualEncoding); } return new JarArchiveOutputStream(out); } if (CPIO.equalsIgnoreCase(archiverName)) { if (actualEncoding != null) { return new CpioArchiveOutputStream(out, actualEncoding); } return new CpioArchiveOutputStream(out); } if (SEVEN_Z.equalsIgnoreCase(archiverName)) { throw new StreamingNotSupportedException(SEVEN_Z); } final ArchiveStreamProvider archiveStreamProvider = getArchiveOutputStreamProviders() .get(toKey(archiverName)); if (archiveStreamProvider != null) { return archiveStreamProvider.createArchiveOutputStream(archiverName, out, actualEncoding); } throw new ArchiveException("Archiver: " + archiverName + " not found."); }
From source file:org.beangle.commons.archiver.ZipUtils.java
public static File zip(List<String> fileNames, String zipPath, String encoding) { try {/* www . j a va 2s . c o m*/ FileOutputStream f = new FileOutputStream(zipPath); ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f); if (null != encoding) { zos.setEncoding(encoding); } for (int i = 0; i < fileNames.size(); i++) { String fileName = fileNames.get(i); String entryName = StringUtils.substringAfterLast(fileName, File.separator); ZipArchiveEntry entry = new ZipArchiveEntry(entryName); zos.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fileName); IOUtils.copy(fis, zos); fis.close(); zos.closeArchiveEntry(); } zos.close(); return new File(zipPath); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.beangle.ems.util.ZipUtils.java
/** * <p>//from w ww. j a v a 2s.c o m * zip. * </p> * * @param fileNames a {@link java.util.List} object. * @param zipPath a {@link java.lang.String} object. * @param encoding a {@link java.lang.String} object. * @return a {@link java.io.File} object. */ public static File zip(List<String> fileNames, String zipPath, String encoding) { try { FileOutputStream f = new FileOutputStream(zipPath); ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f); if (null != encoding) { zos.setEncoding(encoding); } for (int i = 0; i < fileNames.size(); i++) { String fileName = fileNames.get(i); String entryName = Strings.substringAfterLast(fileName, File.separator); ZipArchiveEntry entry = new ZipArchiveEntry(entryName); zos.putArchiveEntry(entry); FileInputStream fis = new FileInputStream(fileName); IOs.copy(fis, zos); fis.close(); zos.closeArchiveEntry(); } zos.close(); return new File(zipPath); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.dataconservancy.packaging.tool.impl.ZipArchiveStreamFactory.java
public ZipArchiveOutputStream newArchiveOutputStream(OutputStream out) { ZipArchiveOutputStream zipOs = new ZipArchiveOutputStream(out); zipOs.setEncoding(encoding); zipOs.setFallbackToUTF8(fallbackToUTF8); zipOs.setUseLanguageEncodingFlag(useLanguageEncodingFlag); zipOs.setLevel(level);/*w w w . jav a 2s .c o m*/ zipOs.setMethod(method); zipOs.setUseZip64(useZip64); return zipOs; }
From source file:org.dbflute.helper.io.compress.DfZipArchiver.java
/** * Compress the directory's elements to archive file. * @param baseDir The base directory to compress. (NotNull) * @param filter The file filter, which doesn't need to accept the base directory. (NotNull) *///from w w w. j a v a 2s . c om public void compress(File baseDir, FileFilter filter) { if (baseDir == null) { String msg = "The argument 'baseDir' should not be null."; throw new IllegalArgumentException(msg); } if (!baseDir.isDirectory()) { String msg = "The baseDir should be directory but not: " + baseDir; throw new IllegalArgumentException(msg); } if (!baseDir.exists()) { String msg = "Not found the baseDir in the file system: " + baseDir; throw new IllegalArgumentException(msg); } OutputStream out = null; ZipArchiveOutputStream archive = null; try { out = new BufferedOutputStream(new FileOutputStream(_zipFile)); archive = new ZipArchiveOutputStream(out); archive.setEncoding("UTF-8"); addAll(archive, baseDir, baseDir, filter); archive.finish(); archive.flush(); out.flush(); } catch (IOException e) { String msg = "Failed to compress the files to " + _zipFile.getPath(); throw new IllegalStateException(msg, e); } finally { if (archive != null) { try { archive.close(); } catch (IOException ignored) { } } if (out != null) { try { out.close(); } catch (IOException ignored) { } } } }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Zip the given src into the given output stream. * // www.jav a2 s .c o m * @param src * src to be zipped * @param os * output stream * @param charsetName * character set to be used * @param includeSrc * true if src will be included. * @throws IOException * IOException */ public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName); FileInputStream fis; int length; ZipArchiveEntry ze; byte[] buf = new byte[8 * 1024]; String name; Stack<File> stack = new Stack<File>(); File root; if (src.isDirectory()) { if (includeSrc) { stack.push(src); root = src.getParentFile(); } else { File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { stack.push(fs[i]); } root = src; } } else { stack.push(src); root = src.getParentFile(); } while (!stack.isEmpty()) { File f = stack.pop(); name = toPath(root, f); if (f.isDirectory()) { File[] fs = f.listFiles(); for (int i = 0; i < fs.length; i++) { if (fs[i].isDirectory()) { stack.push(fs[i]); } else { stack.add(0, fs[i]); } } } else { ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } fis.close(); zos.closeArchiveEntry(); } } zos.close(); }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Zip the given src into the given output stream. * * @param src src to be zipped/*w ww .j a v a2 s . c o m*/ * @param os output stream * @param charsetName character set to be used * @param includeSrc true if src will be included. * @throws IOException IOException */ public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName); FileInputStream fis = null; int length; ZipArchiveEntry ze; byte[] buf = new byte[8 * 1024]; String name; Stack<File> stack = new Stack<File>(); File root; if (src.isDirectory()) { if (includeSrc) { stack.push(src); root = src.getParentFile(); } else { File[] fs = checkNotNull(src.listFiles()); for (int i = 0; i < fs.length; i++) { stack.push(fs[i]); } root = src; } } else { stack.push(src); root = src.getParentFile(); } while (!stack.isEmpty()) { File f = stack.pop(); name = toPath(root, f); if (f.isDirectory()) { File[] fs = checkNotNull(f.listFiles()); for (int i = 0; i < fs.length; i++) { if (fs[i].isDirectory()) { stack.push(fs[i]); } else { stack.add(0, fs[i]); } } } else { ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); try { fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } } finally { IOUtils.closeQuietly(fis); } zos.closeArchiveEntry(); } } zos.close(); }
From source file:org.ngrinder.script.util.CompressionUtil.java
public void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(charsetName); FileInputStream fis;//from ww w .j a v a2s . c o m int length; ZipArchiveEntry ze; byte[] buf = new byte[8 * 1024]; String name; Stack<File> stack = new Stack<File>(); File root; if (src.isDirectory()) { if (includeSrc) { stack.push(src); root = src.getParentFile(); } else { File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { stack.push(fs[i]); } root = src; } } else { stack.push(src); root = src.getParentFile(); } while (!stack.isEmpty()) { File f = stack.pop(); name = toPath(root, f); if (f.isDirectory()) { File[] fs = f.listFiles(); for (int i = 0; i < fs.length; i++) { if (fs[i].isDirectory()) stack.push(fs[i]); else stack.add(0, fs[i]); } } else { ze = new ZipArchiveEntry(name); zos.putArchiveEntry(ze); fis = new FileInputStream(f); while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } fis.close(); zos.closeArchiveEntry(); } } zos.close(); }