将给定目录的所有文件写入指定的jar文件。
/* * Created on 24-Feb-2004 at 16:35:32. * * Copyright (c) 2004-2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * J2ME Polish is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */ //package com.nowjava; import java.io.*; /**来自 时代Java公众号 - N o w J a v a . c o m**/ import java.util.jar.*; import java.util.zip.CRC32; import java.util.zip.ZipOutputStream; public class Main { /** * Writes all files of the given directory to the specified jar-file. * * @param sourceDir The directory containing the "source" files. * @param target The jar file which should be created * @param compress True when the jar file should be compressed * @throws FileNotFoundException when a file could not be found * @throws IOException when a file could not be read or the jar file could not be written to. */ public static void jar(File sourceDir, File target, boolean compress) throws FileNotFoundException, IOException { // create target directory if necessary: if (!target.getParentFile().exists()) {/** 来 自 nowjava**/ target.getParentFile().mkdirs(); } // creates target-jar-file: JarOutputStream out = new JarOutputStream(new FileOutputStream( target)); if (compress) { out.setLevel(ZipOutputStream.DEFLATED); } else { out.setLevel(ZipOutputStream.STORED); } // create a CRC32 object: CRC32 crc = new CRC32(); byte[] buffer = new byte[1024 * 1024]; // read all files from the source directory: File[] fileNames = sourceDir.listFiles(); int sourceDirLength = sourceDir.getAbsolutePath().length() + 1; for (int i = 0; i < fileNames.length; i++) { File file = fileNames[i]; addFile(file, out, crc, sourceDirLength, buffer); } out.close(); } /** * Adds one file to the given jar file. * If the specified file is a directory, all included files will be added. * * @param file The file which should be added * @param out The jar file to which the given jar file should be added * @param crc A helper class for the CRC32 calculation * @param sourceDirLength The number of chars which can be skipped from the file's path * @param buffer A buffer for reading the files. * @throws FileNotFoundException when the file was not found * @throws IOException when the file could not be read or not be added */ private static void addFile(File file, JarOutputStream out, CRC32 crc, int sourceDirLength, byte[] buffer) throws FileNotFoundException, IOException { if (file.isDirectory()) { File[] fileNames = file.listFiles(); for (int i = 0; i < fileNames.length; i++) { addFile(fileNames[i], out, crc, sourceDirLength, buffer); } } else {