Java examples for Reflection:Jar
Adds one file to the given jar file.
/*/*w w w .ja v a2 s . c om*/ * 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.java2s; import java.io.*; import java.util.jar.*; import java.util.zip.CRC32; public class Main { /** * 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 { String entryName = file.getAbsolutePath().substring( sourceDirLength); JarEntry entry = new JarEntry(entryName); out.putNextEntry(entry); // read file: FileInputStream in = new FileInputStream(file); int read; long size = 0; while ((read = in.read(buffer)) != -1) { crc.update(buffer, 0, read); out.write(buffer, 0, read); size += read; } entry.setCrc(crc.getValue()); entry.setSize(size); in.close(); out.closeEntry(); crc.reset(); } } }