Skip to content

Commit

Permalink
fix: always close the ZIP file after reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamik423 authored and rdeltour committed Dec 30, 2024
1 parent 8e21138 commit 96a6e1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public boolean apply(OPFHandler opfHandler)
//
checkFileExtension(state);

// Close zip file to free resource
if (state.getZipResources() != null)
{
try
{
state.getZipResources().close();
}
catch (Exception e)
{
// FIXME 2023 - Inability to close zip file should be handled
}
}
}

private boolean checkContainerFile(OCFCheckerState state)
Expand Down Expand Up @@ -265,7 +277,9 @@ private boolean checkContainerStructure(OCFCheckerState state)
{
// FIXME 2022 build resourcesProvider depending on MIME type
// Get a container
Iterable<OCFResource> resourcesProvider = new OCFZipResources(context.url);
OCFZipResources resourcesProvider = new OCFZipResources(context.url);
// Store the OCFZipResources object so it can be closed later
state.setZipResources(resourcesProvider);
// Set to store the normalized paths for duplicate checks
final Set<String> normalizedPaths = new HashSet<>();
// Lists to store the container entries for later empty directory check
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/adobe/epubcheck/ocf/OCFCheckerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class OCFCheckerState
private final ImmutableList.Builder<URL> packageDocuments = ImmutableList.builder();
private URL mappingDocument;

private OCFZipResources zipResources = null;

private final Map<URL, EPUBLocation> obfuscated = new HashMap<>();
private final Map<URL, Set<PublicationType>> publicationTypes = new LinkedHashMap<>();
private final Map<URL, String> publicationIDs = new LinkedHashMap<>();
Expand Down Expand Up @@ -84,6 +86,16 @@ public void addResource(OCFResource resource)
containerNeedsRebuild = true;
}

public OCFZipResources getZipResources()
{
return zipResources;
}

public void setZipResources(OCFZipResources zipResources)
{
this.zipResources = zipResources;
}

public void addRootfile(String mediaType, URL resource)
{
Preconditions.checkNotNull(mediaType);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/adobe/epubcheck/ocf/OCFZipResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public String toString()
};
}

public void close() throws IOException
{
zip.close();
}

private static String getSHAHash(ZipEntry entry, ZipFile zip)
{
try (InputStream inputStream = zip.getInputStream(entry))
Expand Down

0 comments on commit 96a6e1c

Please sign in to comment.