List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setFallbackToUTF8
public void setFallbackToUTF8(boolean b)
From source file:org.betaconceptframework.astroboa.test.engine.AbstractRepositoryTest.java
protected void serializeUsingJCR(CmsEntityType cmsEntity) { long start = System.currentTimeMillis(); String repositoryHomeDir = AstroboaClientContextHolder.getActiveClientContext().getRepositoryContext() .getCmsRepository().getRepositoryHomeDirectory(); File serializationHomeDir = new File( repositoryHomeDir + File.separator + CmsConstants.SERIALIZATION_DIR_NAME); File zipFile = new File(serializationHomeDir, "document" + DateUtils.format(Calendar.getInstance(), "ddMMyyyyHHmmss.sss") + ".zip"); OutputStream out = null;/* w w w . j a v a2 s . c om*/ ZipArchiveOutputStream os = null; try { if (!zipFile.exists()) { FileUtils.touch(zipFile); } out = new FileOutputStream(zipFile); os = (ZipArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("zip", out); os.setFallbackToUTF8(true); //Serialize all repository using JCR os.putArchiveEntry(new ZipArchiveEntry("document-view.xml")); final Session session = getSession(); switch (cmsEntity) { case OBJECT: session.exportDocumentView(JcrNodeUtils.getContentObjectRootNode(session).getPath(), os, false, false); break; case REPOSITORY_USER: session.exportDocumentView(JcrNodeUtils.getRepositoryUserRootNode(session).getPath(), os, false, false); break; case TAXONOMY: session.exportDocumentView(JcrNodeUtils.getTaxonomyRootNode(session).getPath(), os, false, false); break; case ORGANIZATION_SPACE: session.exportDocumentView(JcrNodeUtils.getOrganizationSpaceNode(session).getPath(), os, false, false); break; case REPOSITORY: session.exportDocumentView(JcrNodeUtils.getCMSSystemNode(session).getPath(), os, false, false); break; default: break; } os.closeArchiveEntry(); os.finish(); os.close(); } catch (Exception e) { throw new CmsException(e); } finally { if (out != null) { IOUtils.closeQuietly(out); } if (os != null) { IOUtils.closeQuietly(os); } long serialzationDuration = System.currentTimeMillis() - start; logger.debug("Export entities using JCR finished in {} ", DurationFormatUtils.formatDurationHMS(serialzationDuration)); } }
From source file:org.dataconservancy.packaging.tool.impl.ZipArchiveStreamFactory.java
public ZipArchiveOutputStream newArchiveOutputStream(OutputStream out) { ZipArchiveOutputStream zipOs = new ZipArchiveOutputStream(out); zipOs.setEncoding(encoding);/*from ww w .ja v a2 s.c o m*/ zipOs.setFallbackToUTF8(fallbackToUTF8); zipOs.setUseLanguageEncodingFlag(useLanguageEncodingFlag); zipOs.setLevel(level); zipOs.setMethod(method); zipOs.setUseZip64(useZip64); return zipOs; }
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 . c om 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:server.Folder.java
/** * Create a zipped folder containing those OSDs and subfolders (recursively) which the * validator allows. <br/>/*from w w w . j ava 2 s . 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; }