1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-13 19:27:33 +01:00

fix bug where reading files of ZIP archive out of order would not make them available

This commit is contained in:
MPeter 2022-09-02 14:24:17 +02:00
parent ffcb67636e
commit a0782d318b
2 changed files with 8 additions and 15 deletions

View File

@ -59,7 +59,6 @@ public class PineTimeInstallHandler implements InstallHandler {
ZipFile dfuPackage = new ZipFile(uriHelper.openInputStream()); ZipFile dfuPackage = new ZipFile(uriHelper.openInputStream());
String manifest = new String(dfuPackage.getFileFromZip("manifest.json")); String manifest = new String(dfuPackage.getFileFromZip("manifest.json"));
dfuPackage.close();
if (!manifest.trim().isEmpty()) { if (!manifest.trim().isEmpty()) {
dfuPackageManifest = new Gson().fromJson(manifest.trim(), InfiniTimeDFUPackage.class); dfuPackageManifest = new Gson().fromJson(manifest.trim(), InfiniTimeDFUPackage.class);
@ -111,7 +110,6 @@ public class PineTimeInstallHandler implements InstallHandler {
LOG.debug("Initialized PineTimeInstallHandler"); LOG.debug("Initialized PineTimeInstallHandler");
} }
@Override @Override
public void onStartInstall(GBDevice device) { public void onStartInstall(GBDevice device) {
} }

View File

@ -13,28 +13,28 @@ import java.util.zip.ZipInputStream;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
public class ZipFile implements AutoCloseable { public class ZipFile {
private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class); private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class);
public static final byte[] ZIP_HEADER = new byte[]{ public static final byte[] ZIP_HEADER = new byte[]{
0x50, 0x4B, 0x03, 0x04 0x50, 0x4B, 0x03, 0x04
}; };
private final ZipInputStream zipInputStream; private final byte[] zipBytes;
/** /**
* Open ZIP file from byte array in memory * Open ZIP file from byte array in memory
* @param zipBytes data to handle as a ZIP file * @param zipBytes data to handle as a ZIP file
*/ */
public ZipFile(byte[] zipBytes) { public ZipFile(byte[] zipBytes) {
zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes)); this.zipBytes = zipBytes;
} }
/** /**
* Open ZIP file from InputStream * Open ZIP file from InputStream
* @param inputStream data to handle as a ZIP file * @param inputStream data to handle as a ZIP file
*/ */
public ZipFile(InputStream inputStream) { public ZipFile(InputStream inputStream) throws IOException {
zipInputStream = new ZipInputStream(inputStream); this.zipBytes = readAllBytes(inputStream);
} }
/** /**
@ -54,7 +54,7 @@ public class ZipFile implements AutoCloseable {
* @throws ZipFileException If the specified path does not exist or references a directory, or if some other I/O error occurs. In other words, if return value would otherwise be null. * @throws ZipFileException If the specified path does not exist or references a directory, or if some other I/O error occurs. In other words, if return value would otherwise be null.
*/ */
public byte[] getFileFromZip(final String path) throws ZipFileException { public byte[] getFileFromZip(final String path) throws ZipFileException {
try { try (InputStream is = new ByteArrayInputStream(zipBytes); ZipInputStream zipInputStream = new ZipInputStream(is)) {
ZipEntry zipEntry; ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) { while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague. if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague.
@ -86,8 +86,8 @@ public class ZipFile implements AutoCloseable {
@Deprecated @Deprecated
@Nullable @Nullable
public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) { public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) {
try (ZipFile zip = new ZipFile(zipBytes)) { try {
return zip.getFileFromZip(path); return new ZipFile(zipBytes).getFileFromZip(path);
} catch (ZipFileException e) { } catch (ZipFileException e) {
LOG.error("Quick ZIP reading failed.", e); LOG.error("Quick ZIP reading failed.", e);
} catch (Exception e) { } catch (Exception e) {
@ -109,9 +109,4 @@ public class ZipFile implements AutoCloseable {
return buffer.toByteArray(); return buffer.toByteArray();
} }
@Override
public void close() throws Exception {
zipInputStream.close();
}
} }