Java tutorial
/* * Copyright (c) 2009 The openGion Project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.opengion.fukurou.util; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; import java.util.ArrayList; import java.util.List; // import java.util.zip.ZipEntry; // import java.util.zip.ZipInputStream; // import java.util.zip.ZipOutputStream; /** * ZipArchive.java ??ZIP??????Util?? * <p> * zip???????????? * ZIP?????java.util.zip ????? * ????????UTF-8??Windows??? * ?????????????????? * ????????????????? * ????????ant.jar ????org.apache.tools.zip ??Apache Commons ? * org.apache.commons.compress ?? * org.apache.tools.zip ??java.util.zip ????????????? * ???????? * openGion ???????????org.apache.commons.compress * ??? * * @author Kazuhiko Hasegawa * @version 6.0 * @og.group * @og.rev 6.0.0.0 (2014/04/11) org.apache.commons.compress ?(??) * @since JDK5.0, */ public final class ZipArchive { /** * ?????????? */ private ZipArchive() { } /** * ??Windows-31J ???ZIP?????? * ?(targetPath)???ZIP(zipFile)???? * ????????????????? * * @param targetPath ?? * @param zipFile ??ZIP * @return ???ZIP? * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??) */ public static List<File> unCompress(final File targetPath, final File zipFile) { return unCompress(targetPath, zipFile, "Windows-31J"); } /** * ???ZIP?????? * ?(targetPath)???ZIP(zipFile)???? * ????????????????? * <p> * ??????????????? * * @param targetPath ?? * @param zipFile ??ZIP * @param encording ?(Windows???"Windows-31J" ???) * @return ???ZIP? * @og.rev 4.1.0.2 (2008/02/01) ? * @og.rev 4.3.1.1 (2008/08/23) mkdirs ? * @og.rev 4.3.3.3 (2008/10/22) mkdirs???? * @og.rev 5.1.9.0 (2010/08/01) ? * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??) */ public static List<File> unCompress(final File targetPath, final File zipFile, final String encording) { List<File> list = new ArrayList<File>(); // ???'/'??'\'???? // String tmpPrefix = targetPath; // if( File.separatorChar != targetPath.charAt( targetPath.length() - 1 ) ) { // tmpPrefix = tmpPrefix + File.separator; // } ZipArchiveInputStream zis = null; File tmpFile = null; // String fileName = null; try { zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)), encording); ZipArchiveEntry entry = null; while ((entry = zis.getNextZipEntry()) != null) { // fileName = tmpPrefix + entry.getName().replace( '/', File.separatorChar ); tmpFile = new File(targetPath, entry.getName()); list.add(tmpFile); // ?????? if (entry.isDirectory()) { if (!tmpFile.exists() && !tmpFile.mkdirs()) { String errMsg = "??????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } } // ???????? else { // 4.3.3.3 (2008/10/22) ????? if (!tmpFile.getParentFile().exists() && !tmpFile.getParentFile().mkdirs()) { String errMsg = "??????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile)); try { IOUtils.copy(zis, out); } catch (IOException zex) { String errMsg = "ZIP??(copy)?????[??=" + tmpFile + "]"; System.err.println(errMsg); continue; } finally { Closer.ioClose(out); } } // 5.1.9.0 (2010/08/01) ? long lastTime = entry.getTime(); if (lastTime >= 0 && !tmpFile.setLastModified(lastTime)) { String errMsg = "ZIP??????[??=" + tmpFile + "]"; System.err.println(errMsg); } } } catch (FileNotFoundException ex) { String errMsg = "????????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } catch (IOException ex) { String errMsg = "ZIP???????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } finally { Closer.ioClose(zis); } return list; } /** * ???????ZIP???? * ??DEFAULT_COMPRESSION?? * ???????????CRC? * ????????????????????(????? * ???????) * ??????????????????????? * ?ZIP??????????????? * * @param files ?? * @param zipFile ZIP?? * @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) { return compress(files, zipFile, "Windows-31J"); } /** * ???????ZIP???? * ??DEFAULT_COMPRESSION?? * ???????????CRC? * ????????????????????(????? * ???????) * ??????????????????????? * ?ZIP??????????????? * * @param dir ??? * @param zipFile ZIP?? * @return ZIP??? * @og.rev 5.7.1.2 (2013/12/20) org.apache.commons.compress ?(??) */ public static List<File> compress(final File dir, final File zipFile) { File[] files = null; if (dir.isDirectory()) { files = dir.listFiles(); } else { files = new File[] { dir }; } // ???????????? return compress(files, zipFile, "Windows-31J"); } /** * ???????ZIP???? * ??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; } /** * ZIP???? * ???File????????? * ??????????????? * ?????? * * @param list ZIP??? * @param zos ZIPOutputStream * @param prefix ? * @param files ?? * @throws IOException ???? * @og.rev 4.1.0.2 (2008/02/01) ? * @og.rev 5.1.9.0 (2010/08/01) ? ?BufferedInputStream ?????? */ private static void addZipEntry(final List<File> list, final ZipArchiveOutputStream zos, final String prefix, final File[] files) { File tmpFile = null; try { for (File fi : files) { tmpFile = fi; // ? list.add(fi); if (fi.isDirectory()) { String entryName = prefix + fi.getName() + "/"; ZipArchiveEntry zae = new ZipArchiveEntry(entryName); zos.putArchiveEntry(zae); zos.closeArchiveEntry(); addZipEntry(list, zos, entryName, fi.listFiles()); } else { String entryName = prefix + fi.getName(); ZipArchiveEntry zae = new ZipArchiveEntry(entryName); zos.putArchiveEntry(zae); InputStream is = new BufferedInputStream(new FileInputStream(fi)); IOUtils.copy(is, zos); zos.closeArchiveEntry(); Closer.ioClose(is); } } } catch (FileNotFoundException ex) { String errMsg = "??????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } catch (IOException ex) { String errMsg = "ZIP?????[??=" + tmpFile + "]"; throw new RuntimeException(errMsg, ex); } } /** * ???????? * * @param args * @og.rev 4.1.0.2 (2008/02/01) ? * <p> * Usage: java org.opengion.fukurou.util.ZipArchive comp|uncomp targetPath zipFileName * 1 : comp: uncomp:? * 2 : ZIP?? * 3 : :??? ?:?? */ public static void main(final String[] args) { String usage = "Usage: java org.opengion.fukurou.util.ZipArchive comp|uncomp targetPath zipFileName"; if (args.length < 3) { System.out.println(usage); return; } // long start = System.currentTimeMillis(); List<File> list = null; File tgtFile = new File(args[1]); File zipFile = new File(args[2]); if ("comp".equalsIgnoreCase(args[0])) { list = compress(tgtFile, zipFile); } else if ("uncomp".equalsIgnoreCase(args[0])) { list = unCompress(tgtFile, zipFile); } else { System.out.println(usage); return; } if (list != null) { // ? System.out.println("? : " + (System.currentTimeMillis() - start) + "(ms)"); // ? for (File fileName : list) { System.out.println(fileName); } } } }