集册 Java实例教程 读取包裹

读取包裹

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

510
从给定的jar文件中读取软件包名称。

/*

 * Copyright @ 2006-2010 by The Jxva Framework Foundation

 * 

 * 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 com.nowjava;

import java.io.File;/**时 代 J     a    v  a - nowjava.com**/


import java.io.IOException;


import java.util.Enumeration;

import java.util.HashMap;


import java.util.jar.JarEntry;

import java.util.jar.JarFile;


import java.util.zip.ZipFile;


public class Main {

    /**

     * Reads the package-names from the given jar-file.

     * 

     * @param jarFile the jar file

     * @return an array with all found package-names

     * @throws IOException when the jar-file could not be read

     */

    public static String[] getPackageNames(File jarFile) throws IOException {

        HashMap<String, String> packageNames = new HashMap<String, String>();

        JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);

        Enumeration<JarEntry> enumeration = input.entries();

        for (; enumeration.hasMoreElements();) {
        /* from 
        nowjava*/

            JarEntry entry = enumeration.nextElement();

            String name = entry.getName();

            if (name.endsWith(".class")) {

                int endPos = name.lastIndexOf('/');

                boolean isWindows = false;

                if (endPos == -1) {

                    endPos = name.lastIndexOf('\\');

                    isWindows = true;

                }

                name = name.substring(0, endPos);

               
展开阅读全文