List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setCreateUnicodeExtraFields
public void setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy b)
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()); }/*from ww w . j a va 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 . j a va 2s . c o 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 w w. j ava 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; }