psiegman / epublib

a java library for reading and writing epub files
http://www.siegmann.nl/epublib
1.06k stars 317 forks source link

Major bug, there is a dead cycle in parsing #149

Open DearMrT opened 1 year ago

DearMrT commented 1 year ago
private static final ZipEntry ERROR_ZIP_ENTRY = new ZipEntry("<error>");

private static ZipEntry getNextZipEntry(ZipInputStream zipInputStream) throws IOException {
    ZipEntry result = ERROR_ZIP_ENTRY;
    try {
        result = zipInputStream.getNextEntry();
    } catch(ZipException e) {
        LOG.error(e.getMessage());
        zipInputStream.closeEntry();
    }
    return result;
}

public static Resources loadResources(ZipInputStream zipInputStream, String defaultHtmlEncoding) throws IOException {
    Resources result = new Resources();
    ZipEntry zipEntry;
    do {
        // get next valid zipEntry
        zipEntry = getNextZipEntry(zipInputStream);
        if((zipEntry == null) || (zipEntry == ERROR_ZIP_ENTRY) || zipEntry.isDirectory()) {
            continue;
        }

        // store resource
        Resource resource = ResourceUtil.createResource(zipEntry, zipInputStream);
        if(resource.getMediaType() == MediatypeService.XHTML) {
            resource.setInputEncoding(defaultHtmlEncoding);
        }
        result.add(resource);
    } while(zipEntry != null);

    return result;
}