Конвертировать zip-файл с рекурсивным подкаталогом в объект - PullRequest
0 голосов
/ 23 сентября 2018

Мне нужно прочитать zip-файл и преобразовать его содержимое в объект Bundle.Каталог SUB_BUNDLE является рекурсивным.

Содержимое файла:

file1.json
file2.xml
file3.xml
SUB_BUNDLE/w2/file4.json
SUB_BUNDLE/w2/file5.json
SUB_BUNDLE/w2/PDF_TEMPLATE/
SUB_BUNDLE/w2/PDF_TEMPLATE/file6.pdf
SUB_BUNDLE/w3/file7.json
SUB_BUNDLE/w3/file8.json
SUB_BUNDLE/w3/SUB_BUNDLE/w4/file9.json
SUB_BUNDLE/w3/SUB_BUNDLE/w4/file10.json
SUB_BUNDLE/w5/file11.json
PDF_TEMPLATE/file12.pdf

Я пытаюсь создать объект Bundle из данных каталога, как показано выше:

files: [ 'file1.json', 'file2.xml', 'file3.xml' ],
pdfTemplates: [ 'file12.pdf' ],
subBundles: [
    w2: {
        files: [ 'file4.json', 'file5.json' ],
        subBundles: [ ],
        pdfTemplates: [ 'file6.pdf' ]
    }
    w3: {
        files: [ 'file7.json', 'file8.json' ],
        pdfTemplates: [ ],
        subBundles: [
            w4: {
                files: [ 'file9.json', 'file10.json' ],
                subBundles: [ ],
                pdfTemplates: [ ]
            }
         ]
    }
    w5: {
        files: [ 'file11.json' ],
        subBundles: [ ],
        pdfTemplates: [ ]
    }
]

Класс Bundle

public class Bundle () {
    protected List<Bundle> files = new ArrayList();
    protected List<Bundle> pdfTemplates = new ArrayList();
    protected List<Bundle> subBundles = new ArrayList();

    public Bundle () { }

    public void addSubBundle(Bundle bundle) {
        subBundles.add(bundle);
    }
    public void addPdfTemplate(String pdfTemplate) {
        pdfTemplates.add(pdfTemplate);
    }
    public void addFile(String file) {
        files.add(file);
    }
}

Я пробовал много вещей, но это моя отправная точка, которая будет возвращать объект Bundle, как описано выше:

public Bundle unpack(InputStream inputStream, Bundle bundle) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry entry;

    while ((entry = zipInputStream.getNextEntry()) != null) {
        final String entryName = entry.getName();

        if (entry.isDirectory()) {
            final String[] parts = entryName.split("/");
            final String folder = parts[parts.length - 1];

            if(folder.equlas("PDF_TEMPLATE")) {
                bundle.addPdfTemplate(entryName);
            } else {
                Bundle newBundle = new Bundle();
                // fill this new bundle's file,pdfTepmlates and subBundles and then add into its parent as subSubdle
                bundle.addSubBundle(newBundle);
            }

        } else {
            bundle.addFile(entryName);
        }
    }

    return bundle;
}

Я знаю, что мне нужно где-то рекурсивный цикл, ноЯ не мог сделать это.Не могли бы вы помочь мне в этом?

...