List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setEncoding
public void setEncoding(final String encoding)
From source file:org.opengion.fukurou.util.ZipArchive.java
/** * ???????ZIP????/*from w ww .j a va 2 s. c om*/ * ??DEFAULT_COMPRESSION?? * ???????????CRC? * ????????????????????(????? * ???????) * ??????????????????????? * ?ZIP??????????????? * * @param files ?? * @param zipFile ZIP?? * @param encording ?(Windows???"Windows-31J" ???) * @return ZIP??? * @og.rev 4.1.0.2 (2008/02/01) ? * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??) */ public static List<File> compress(final File[] files, final File zipFile, final String encording) { List<File> list = new ArrayList<File>(); ZipArchiveOutputStream zos = null; try { zos = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); if (encording != null) { zos.setEncoding(encording); // "Windows-31J" } // ZIP???? addZipEntry(list, zos, "", files); // ?????? } catch (FileNotFoundException ex) { String errMsg = "ZIP?????[??=" + zipFile + "]"; throw new RuntimeException(errMsg, ex); } finally { Closer.ioClose(zos); // zos.finish(); // zos.flush(); // zos.close(); } return list; }
From source file:org.sourcepit.tools.shared.resources.internal.mojo.ZipUtils.java
public static void zip(File directory, File archive, String encoding) throws IOException { createFileOnDemand(archive);/*from w ww. j a v a 2 s. c o m*/ final int pathOffset = getAbsolutePathLength(directory); final ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(archive); zipOut.setEncoding(encoding); try { for (File file : directory.listFiles()) { appendFileOrDirectory(pathOffset, zipOut, file); } } finally { IOUtils.closeQuietly(zipOut); } }
From source file:org.springframework.boot.gradle.tasks.bundling.BootZipCopyAction.java
@Override public WorkResult execute(CopyActionProcessingStream stream) { ZipArchiveOutputStream zipStream; Spec<FileTreeElement> loaderEntries; try {/*from w w w .j a v a 2 s.c o m*/ FileOutputStream fileStream = new FileOutputStream(this.output); writeLaunchScriptIfNecessary(fileStream); zipStream = new ZipArchiveOutputStream(fileStream); if (this.encoding != null) { zipStream.setEncoding(this.encoding); } loaderEntries = writeLoaderClassesIfNecessary(zipStream); } catch (IOException ex) { throw new GradleException("Failed to create " + this.output, ex); } try { stream.process(new ZipStreamAction(zipStream, this.output, this.preserveFileTimestamps, this.requiresUnpack, createExclusionSpec(loaderEntries), this.compressionResolver)); } finally { try { zipStream.close(); } catch (IOException ex) { // Continue } } return () -> true; }
From source file:org.structr.web.function.CreateArchiveFunction.java
@Override public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException { if (!(sources[1] instanceof File || sources[1] instanceof Folder || sources[1] instanceof Collection || sources.length < 2)) { logParameterError(caller, sources, ctx.isJavaScriptContext()); return usage(ctx.isJavaScriptContext()); }// w w w.j av a 2s . co m final ConfigurationProvider config = StructrApp.getConfiguration(); try { java.io.File newArchive = java.io.File.createTempFile(sources[0].toString(), "zip"); ZipArchiveOutputStream zaps = new ZipArchiveOutputStream(newArchive); zaps.setEncoding("UTF8"); zaps.setUseLanguageEncodingFlag(true); zaps.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); zaps.setFallbackToUTF8(true); if (sources[1] instanceof File) { File file = (File) sources[1]; addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps); } else if (sources[1] instanceof Folder) { Folder folder = (Folder) sources[1]; addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps); addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps); } else if (sources[1] instanceof Collection) { for (Object fileOrFolder : (Collection) sources[1]) { if (fileOrFolder instanceof File) { File file = (File) fileOrFolder; addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps); } else if (fileOrFolder instanceof Folder) { Folder folder = (Folder) fileOrFolder; addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps); addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps); } else { logParameterError(caller, sources, ctx.isJavaScriptContext()); return usage(ctx.isJavaScriptContext()); } } } else { logParameterError(caller, sources, ctx.isJavaScriptContext()); return usage(ctx.isJavaScriptContext()); } zaps.close(); Class archiveClass = null; if (sources.length > 2) { archiveClass = config.getNodeEntityClass(sources[2].toString()); } if (archiveClass == null) { archiveClass = org.structr.web.entity.File.class; } try (final FileInputStream fis = new FileInputStream(newArchive)) { return FileHelper.createFile(ctx.getSecurityContext(), fis, "application/zip", archiveClass, sources[0].toString() + ".zip"); } } catch (IOException e) { logException(caller, e, sources); } return null; }
From source file:org.wso2.carbon.connector.util.FileCompressUtil.java
/** * Compress the files based on the archive type * //from w ww .ja v a2s.co m * * @param files * @param file * @param archiveType * @throws IOException */ public void compressFiles(Collection files, File file, ArchiveType archiveType) throws IOException { log.info("Compressing " + files.size() + " to " + file.getAbsoluteFile()); // Create the output stream for the output file FileOutputStream fos; switch (archiveType) { case TAR_GZIP: fos = new FileOutputStream(new File(file.getCanonicalPath() + ".tar" + ".gz")); // Wrap the output file stream in streams that will tar and gzip // everything TarArchiveOutputStream taos = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(fos))); // TAR has an 8 gig file limit by default, this gets around that taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit; TAR originally didn't support // long file names, so enable the // support for it taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); // Get to putting all the files in the compressed output file Iterator iterator = files.iterator(); while (iterator.hasNext()) { File f = (File) iterator.next(); addFilesToCompression(taos, f, ".", ArchiveType.TAR_GZIP); // do something to object here... } // Close everything up taos.close(); fos.close(); break; case ZIP: fos = new FileOutputStream(new File(file.getCanonicalPath() + ".zip")); // Wrap the output file stream in streams that will tar and zip // everything ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(new BufferedOutputStream(fos)); zaos.setEncoding("UTF-8"); zaos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); // Get to putting all the files in the compressed output file Iterator iterator1 = files.iterator(); while (iterator1.hasNext()) { File f = (File) iterator1.next(); addFilesToCompression(zaos, f, ".", ArchiveType.ZIP); // do something to object here... } // Close everything up zaos.close(); fos.close(); break; } }
From source file:server.Folder.java
/** * Create a zipped folder containing those OSDs and subfolders (recursively) which the * validator allows. <br/>/*from w ww . ja va 2s . c o m*/ * Zip file encoding compatibility is difficult to achieve.<br/> * Using Cp437 as encoding will generate zip archives which can be unpacked with MS Windows XP * system utilities and also with the Linux unzip tool v6.0 (although the unzip tool will list them * as corrupted filenames with "?" in place for the special characters, it should unpack them * correctly). In tests, 7zip was unable to unpack those archives without messing up the filenames * - it requires UTF8 as encoding, as far as I can tell.<br/> * see: http://commons.apache.org/compress/zip.html#encoding<br/> * to manually test this, use: https://github.com/dewarim/GrailsBasedTesting * @param folderDao data access object for Folder objects * @param latestHead if set to true, only add objects with latestHead=true, if set to false include only * objects with latestHead=false, if set to null: include everything regardless of * latestHead status. * @param latestBranch if set to true, only add objects with latestBranch=true, if set to false include only * objects with latestBranch=false, if set to null: include everything regardless of * latestBranch status. * @param validator a Validator object which should be configured for the current user to check if access * to objects and folders inside the given folder is allowed. The content of this folder * will be filtered before it is added to the archive. * @return the zip archive of the given folder */ public ZippedFolder createZippedFolder(FolderDAO folderDao, Boolean latestHead, Boolean latestBranch, Validator validator) { String repositoryName = HibernateSession.getLocalRepositoryName(); final File sysTempDir = new File(System.getProperty("java.io.tmpdir")); File tempFolder = new File(sysTempDir, UUID.randomUUID().toString()); if (!tempFolder.mkdirs()) { throw new CinnamonException(("error.create.tempFolder.fail")); } List<Folder> folders = new ArrayList<Folder>(); folders.add(this); folders.addAll(folderDao.getSubfolders(this, true)); folders = validator.filterUnbrowsableFolders(folders); log.debug("# of folders found: " + folders.size()); // create zip archive: ZippedFolder zippedFolder; try { File zipFile = File.createTempFile("cinnamonArchive", "zip"); zippedFolder = new ZippedFolder(zipFile, this); final OutputStream out = new FileOutputStream(zipFile); ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out); String encoding = ConfThreadLocal.getConf().getField("zipFileEncoding", "Cp437"); log.debug("current file.encoding: " + System.getProperty("file.encoding")); log.debug("current Encoding for ZipArchive: " + zos.getEncoding() + "; will now set: " + encoding); zos.setEncoding(encoding); zos.setFallbackToUTF8(true); zos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); for (Folder folder : folders) { String path = folder.fetchPath().replace(fetchPath(), name); // do not include the parent folders up to root. log.debug("zipFolderPath: " + path); File currentFolder = new File(tempFolder, path); if (!currentFolder.mkdirs()) { // log.warn("failed to create folder for: "+currentFolder.getAbsolutePath()); } List<ObjectSystemData> osds = validator.filterUnbrowsableObjects( folderDao.getFolderContent(folder, false, latestHead, latestBranch)); if (osds.size() > 0) { zippedFolder.addToFolders(folder); // do not add empty folders as they are excluded automatically. } for (ObjectSystemData osd : osds) { if (osd.getContentSize() == null) { continue; } zippedFolder.addToObjects(osd); File outFile = osd.createFilenameFromName(currentFolder); // the name in the archive should be the path without the temp folder part prepended. String zipEntryPath = outFile.getAbsolutePath().replace(tempFolder.getAbsolutePath(), ""); if (zipEntryPath.startsWith(File.separator)) { zipEntryPath = zipEntryPath.substring(1); } log.debug("zipEntryPath: " + zipEntryPath); zipEntryPath = zipEntryPath.replaceAll("\\\\", "/"); zos.putArchiveEntry(new ZipArchiveEntry(zipEntryPath)); IOUtils.copy(new FileInputStream(osd.getFullContentPath(repositoryName)), zos); zos.closeArchiveEntry(); } } zos.close(); } catch (Exception e) { log.debug("Failed to create zipFolder:", e); throw new CinnamonException("error.zipFolder.fail", e.getLocalizedMessage()); } return zippedFolder; }