Как я должен извлечь сжатые папки в Java?

Если вы просто хотите вставить значение в определенную позицию в строке, вы можете использовать метод String.Insert :

public string Insert(
    int startIndex,
    string value
)

Пример :

"abc".Insert(2, "XYZ") == "abXYZc"

8
задан Chathuranga Chandrasekara 2 June 2009 в 11:14
поделиться

1 ответ

Вы можете использовать File.mkdirs () для создания папок. Попробуйте изменить свой метод следующим образом:

public static void getZipFiles(String filename) {
    try {
        String destinationname = "c:\\zip\\";
        byte[] buf = new byte[1024];
        ZipInputStream zipinputstream = null;
        ZipEntry zipentry;
        zipinputstream = new ZipInputStream(
                new FileInputStream(filename));

        zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            //for each entry to be extracted
            String entryName = destinationname + zipentry.getName();
            entryName = entryName.replace('/', File.separatorChar);
            entryName = entryName.replace('\\', File.separatorChar);
            System.out.println("entryname " + entryName);
            int n;
            FileOutputStream fileoutputstream;
            File newFile = new File(entryName);
            if (zipentry.isDirectory()) {
                if (!newFile.mkdirs()) {
                    break;
                }
                zipentry = zipinputstream.getNextEntry();
                continue;
            }

            fileoutputstream = new FileOutputStream(entryName);

            while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }

            fileoutputstream.close();
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();

        }//while

        zipinputstream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
7
ответ дан 5 December 2019 в 21:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: