Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Программирование и базы данных (http://forum.oszone.net/forumdisplay.php?f=21)
-   -   Упаковать в zip папку со всеми вложениями (http://forum.oszone.net/showthread.php?t=327256)

blackeangel 01-06-2017 16:17 2741377

Упаковать в zip папку со всеми вложениями
 
Итак, нашел 2 метода упаковки папок в архив. НО оба с ошибками:
В первом создает файлы размером 0 байт с именем любого каталога:
Код:

public static void zipDirectory(File dir, File zipFile) throws IOException {
    FileOutputStream fout = new FileOutputStream(zipFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zipSubDirectory("", dir, zout);
    zout.close();
 }
    private static void zipSubDirectory(String basePath, File dir, ZipOutputStream zout) throws IOException {
        byte[] buffer = new byte[8000];
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String path = basePath + file.getName() + "/";
                zout.putNextEntry(new ZipEntry(path));
                zipSubDirectory(path, file, zout);
                zout.closeEntry();
            } else {
                FileInputStream fin = new FileInputStream(file);
                zout.putNextEntry(new ZipEntry(basePath + file.getName()));
                int length;
                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }
                zout.closeEntry();
                fin.close();
            }
        }
    }

Второй упаковывает как надо, без призраков, НО - берет лишнюю коневую папку
(При упаковки папки tmp вместо 1/2/3 делает tmp/1/2/3)
Код:

    public static void arhivedir(String zip, String folder) throws Exception {
        FileOutputStream fos = new FileOutputStream(zip);
        ZipOutputStream zos = new ZipOutputStream(fos);
        addDirToZipArchive(zos, new File(folder), null);
        zos.flush();
        fos.flush();
        zos.close();
        fos.close();
    }
    public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName) throws Exception {
        if (fileToZip == null || !fileToZip.exists()) {
            return;
        }
        String zipEntryName = fileToZip.getName();
        if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) {
            zipEntryName = parrentDirectoryName + "/" + fileToZip.getName();
        }
        if (fileToZip.isDirectory()) {
            System.out.println("+" + zipEntryName);
            for (File file : fileToZip.listFiles()) {
                addDirToZipArchive(zos, file, zipEntryName);
            }
        } else {
            System.out.println("  " + zipEntryName);
            byte[] buffer = new byte[4096];
            FileInputStream fis = new FileInputStream(fileToZip);
            zos.putNextEntry(new ZipEntry(zipEntryName));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
            fis.close();
        }
    }

Как решить эти проблемы?

lxa85 01-06-2017 20:24 2741445

Код:

        if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) {
            zipEntryName = parrentDirectoryName + "/" + fileToZip.getName();
        }

Не то?
Отладчик имеется? Что он говорит?


Время: 16:58.

Время: 16:58.
© OSzone.net 2001-