集册 Java实例教程 将一个文件添加到给定的jar文件中。

将一个文件添加到给定的jar文件中。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

621
将一个文件添加到给定的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.*;// 来自 n  o  w  j  a  v  a . c o m


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);

            }/*来 自 NowJava.com - 时  代  Java*/

        } else {

            String entryName = file.getAbsolutePath().substring(

                    sourceDirLength);

            JarEntry entry = new JarEntry(entryName);

            out.putNextEntry(entry);

            // read file:

            FileInputStream in = 
展开阅读全文