Skip to content

Commit

Permalink
Add ZipEntryStorage.getArchivePath() and deprecate its getArchive()
Browse files Browse the repository at this point in the history
This helps to prevent issues like
eclipse-platform/eclipse.platform.ui#2251
  • Loading branch information
HannesWell committed Sep 7, 2024
1 parent 0870681 commit b660a54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -37,7 +38,6 @@
* @since 3.0
* @noextend This class is not intended to be subclassed by clients.
*/
@SuppressWarnings("resource")
public class ZipEntryStorage extends PlatformObject implements IStorage {

/**
Expand Down Expand Up @@ -65,15 +65,15 @@ public ZipEntryStorage(ZipFile archive, ZipEntry entry) {
@Override
public InputStream getContents() throws CoreException {
try {
return getArchive().getInputStream(getZipEntry());
return fArchive.getInputStream(getZipEntry());
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.ERROR, SourceLookupMessages.ZipEntryStorage_0, e));
}
}

@Override
public IPath getFullPath() {
return IPath.fromOSString(getArchive().getName()).append(getZipEntry().getName());
return IPath.fromOSString(fArchive.getName()).append(getZipEntry().getName());
}

@Override
Expand Down Expand Up @@ -106,11 +106,27 @@ private void setArchive(ZipFile archive) {
* Returns the archive containing the zip entry.
*
* @return zip file
* @deprecated Granting free access to the backing zip file archive is
* dangerous because a caller could close it prematurely and
* thus break all subsequent usages. Existing callers should use
* derived methods like {@link #getArchivePath()} or
* {@link #getContents()} instead, if possible.
*/
@Deprecated
public ZipFile getArchive() {
return fArchive;
}

/**
* Returns the path of the archive containing the zip entry in the
* file-system.
*
* @return the zip file's file-system path
*/
public Path getArchivePath() {
return Path.of(fArchive.getName());
}

/**
* Sets the entry that contains the source.
*
Expand All @@ -131,9 +147,9 @@ public ZipEntry getZipEntry() {

@Override
public boolean equals(Object object) {
return object instanceof ZipEntryStorage &&
getArchive().equals(((ZipEntryStorage)object).getArchive()) &&
getZipEntry().getName().equals(((ZipEntryStorage)object).getZipEntry().getName());
return object instanceof ZipEntryStorage other //
&& fArchive.equals(other.fArchive) //
&& getZipEntry().getName().equals(other.getZipEntry().getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ImageDescriptor getImageDescriptor(Object o) {
}
return null;
}
@SuppressWarnings("resource")

@Override
public String getLabel(Object o) {
if (o instanceof LocalFileStorage) {
Expand All @@ -54,7 +54,7 @@ public String getLabel(Object o) {
StringBuilder buffer = new StringBuilder();
buffer.append(storage.getZipEntry().getName());
buffer.append(" - "); //$NON-NLS-1$
buffer.append(storage.getArchive().getName());
buffer.append(storage.getArchivePath());
return buffer.toString();
}
return IInternalDebugCoreConstants.EMPTY_STRING;
Expand Down

0 comments on commit b660a54

Please sign in to comment.