public static final void zip(String compressPath, String[] needCompressPaths) { File compressFile = new File(compressPath); List<File> files = Lists.newArrayList(); for (String needCompressPath : needCompressPaths) { File needCompressFile = new File(needCompressPath); if (!needCompressFile.exists()) { continue; } files.add(needCompressFile); } try { ZipArchiveOutputStream zaos = null; try { zaos = new ZipArchiveOutputStream(compressFile); zaos.setUseZip64(Zip64Mode.AsNeeded); zaos.setEncoding("GBK"); for (File file : files) { addFilesToCompression(zaos, file, ""); } } catch (IOException e) { throw e; } finally { IOUtils.closeQuietly(zaos); } } catch (Exception e) { FileUtils.deleteQuietly(compressFile); throw new RuntimeException("压缩失败", e); } }
protected boolean createEmptyZip(File zipFile) throws ArchiverException { if (!createEmpty) { return true; } ZipArchiveOutputStream zOut = null; try { getLogger().debug("Building MANIFEST-only jar: " + getDestFile().getAbsolutePath()); zOut = new ZipArchiveOutputStream(bufferedOutputStream(fileOutputStream(getDestFile(), "jar"))); zOut.setEncoding(getEncoding()); if (isCompress()) { zOut.setMethod(ZipArchiveOutputStream.DEFLATED); } else { zOut.setMethod(ZipArchiveOutputStream.STORED); } initZipOutputStream(zOut); finalizeZipOutputStream(zOut); } catch (IOException ioe) { throw new ArchiverException( "Could not create almost empty JAR archive (" + ioe.getMessage() + ")", ioe); } finally { // Close the output stream. IOUtil.close(zOut); createEmpty = false; } return true; }
/** * 设置压缩文件输出流 * * @param zaos 压缩文件输出流 * @param encoding 字符集 * @param comment 注释 */ private static void config(ZipArchiveOutputStream zaos, String encoding, String comment) { zaos.setUseZip64(Zip64Mode.AsNeeded); if (ValidateUtilsHelper.isNotBlank(encoding)) { zaos.setEncoding(encoding); } if (ValidateUtilsHelper.isNotBlank(comment)) { zaos.setComment(comment); } }
public String export(OutputStream os, XWikiContext context) throws IOException, XWikiException { if (this.files.size() == 0) { return "No Selected file"; } ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setEncoding(XAR_FILENAME_ENCODING); // By including the unicode extra fields, it is possible to extract XAR-files // containing documents with non-ascii characters in the document name using InfoZIP, // and the filenames will be correctly converted to the character set of the local // file system. zos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); for (int i = 0; i < this.files.size(); i++) { DocumentInfo docinfo = this.files.get(i); XWikiDocument doc = docinfo.getDoc(); addToZip(doc, zos, this.withVersions, context); } addInfosToZip(zos, context); zos.finish(); zos.flush(); return ""; }
public void execute() { List<Throwable> throwables = Lists.newLinkedList(); ZipArchiveOutputStream zos = null; try { zos = (ZipArchiveOutputStream) new ArchiveStreamFactory() .createArchiveOutputStream("zip", new FileOutputStream(target)); zos.setEncoding(encoding); doInternalArchive(source, zos, "", throwables); } catch (Exception e) { throwables.add(e); } finally { IOs.freeQuietly(zos); } if (!throwables.isEmpty()) { throw Lang.comboThrow(throwables); } }
private void start() throws Exception { super.dbInit(); SystemConfigsEntity entity = SystemConfigsDao.get() .selectOnKey(SystemConfig.DATA_EXPORT, AppConfig.get().getSystemName()); if (entity == null) { send("[Fail] create fail. please try again."); return; } // エクスポートデータを格納するディレクトリ AppConfig config = AppConfig.get(); File base = new File(config.getTmpPath()); File dir = new File(base, DATA_DIR); if (dir.exists()) { FileUtil.delete(dir); } dir.mkdirs(); File attach = new File(dir, "attach"); attach.mkdirs(); File userdir = new File(dir, "user"); userdir.mkdirs(); // ナレッジデータを取得 LoginedUser loginedUser = new LoginedUser() { @Override public boolean isAdmin() { return true; } }; KnowledgeLogic knowledgeLogic = KnowledgeLogic.get(); CommentsDao commentsDao = CommentsDao.get(); KnowledgeFilesDao knowledgeFilesDao = KnowledgeFilesDao.get(); List<KnowledgesEntity> knowledges = knowledgeLogic.searchKnowledge("", loginedUser, 0, 100); for (KnowledgesEntity knowledgesEntity : knowledges) { File f = new File( dir, "knowledge-" + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6) + ".xml"); String xml = SerializeUtils.objectToXml(knowledgesEntity); FileUtil.write(f, xml); // ナレッジのコメントを取得 List<CommentsEntity> comments = commentsDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); for (CommentsEntity commentsEntity : comments) { File c = new File( dir, "comment-" + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6) + "-" + StringUtils.zeroPadding(commentsEntity.getKnowledgeId(), 6) + ".xml"); xml = SerializeUtils.objectToXml(commentsEntity); FileUtil.write(c, xml); } List<KnowledgeFilesEntity> files = knowledgeFilesDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); for (KnowledgeFilesEntity knowledgeFilesEntity : files) { KnowledgeFilesEntity file = knowledgeFilesDao.selectOnKey(knowledgeFilesEntity.getFileNo()); File a = new File( attach, "attach-" + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6) + "-" + StringUtils.zeroPadding(knowledgeFilesEntity.getFileNo(), 6) + knowledgeFilesEntity.getFileName()); OutputStream outputStream = new FileOutputStream(a); InputStream inputStream = file.getFileBinary(); try { FileUtil.copy(inputStream, outputStream); } finally { inputStream.close(); outputStream.close(); } } send("[Export] knowledge-" + knowledgesEntity.getKnowledgeId()); } // ユーザ情報を取得 UsersDao usersDao = UsersDao.get(); List<UsersEntity> users = usersDao.selectAll(); for (UsersEntity usersEntity : users) { ExportUser user = new ExportUser(); PropertyUtil.copyPropertyValue(usersEntity, user); File f = new File(userdir, "user-" + StringUtils.zeroPadding(user.getUserId(), 6) + ".xml"); String xml = SerializeUtils.objectToXml(user); FileUtil.write(f, xml); send("[Export] user-" + user.getUserId()); } // zip圧縮 send("[Export] during the compression"); String name = DATA_DIR + ".zip"; File comp = new File(base, name); BufferedOutputStream output = null; ZipArchiveOutputStream os = null; try { output = new BufferedOutputStream(new FileOutputStream(comp)); os = new ZipArchiveOutputStream(output); os.setEncoding("UTF-8"); CompressLogic.get().addZip(os, dir, null); } finally { if (os != null) { os.close(); } if (output != null) { output.close(); } } send("[Export] complete!"); // 圧縮の予約を削除 SystemConfigsDao.get().physicalDelete(entity); }