3

から簡単な名前を取得できるかどうかはわかりZipEntryます...

エントリの を呼び出すとgetName()、フル パス名が取得されます。

ファイルの名前だけを取得する必要があります。

ここでは、ルートを含む完全な名前ではなく、単純な名前を取得する必要があります。

public class ZipFileSample {

    public static void main(String[] args) {

        try {
            ZipFile zip = new ZipFile(new File("C:\\Users\\Levi\\Desktop\\jessica.zip"));

            for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                //Here I need to get the simple name instead the full name with its root
                System.out.println(entry.getName());
            }

        } catch (ZipException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
}
4

2 に答える 2

7

どうですか

new File(entry.getName()).getName()
于 2012-10-05T14:19:25.980 に答える
2

以下のコードで試すことができます (java.lang.StringIndexOutOfBoundsException に対して何らかの予防策を講じる必要があるかもしれません)。また、拡張子がわかっている場合は、いくつかのチェックを強制することもできます

        try {
            ZipFile zip = new ZipFile(new File("F:\\OTHERS\\classes.zip"));
            for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                //Here I need to get the simple name instead the full name with its root
                String name =entry.getName();
                //if( name.endsWith(".java"))
//              {
                    name = name.substring(name.lastIndexOf("/")+1,name.length() );
                    System.out.println(name );
//              }
            }

        } catch (ZipException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }
于 2012-10-05T15:44:51.167 に答える