mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-24 19:06:53 +01:00
moved ZIP file handling functions to a utility class
merge conflict fix
This commit is contained in:
parent
01b457895a
commit
ce18a5a6f8
@ -21,19 +21,15 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ZipUtils;
|
||||
|
||||
|
||||
public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
@ -82,7 +78,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
}
|
||||
|
||||
if (uihhFirmwareZipFile != null && hasChangelog) {
|
||||
final byte[] firmwareBin = getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
|
||||
final byte[] firmwareBin = ZipUtils.getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
|
||||
|
||||
if (isCompatibleFirmwareBin(firmwareBin)) {
|
||||
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
|
||||
@ -96,7 +92,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
return HuamiFirmwareType.INVALID;
|
||||
}
|
||||
|
||||
final byte[] firmwareBin = getFileFromZip(bytes, "META/firmware.bin");
|
||||
final byte[] firmwareBin = ZipUtils.getFileFromZip(bytes, "META/firmware.bin");
|
||||
if (isCompatibleFirmwareBin(firmwareBin)) {
|
||||
return HuamiFirmwareType.FIRMWARE;
|
||||
}
|
||||
@ -195,7 +191,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
}
|
||||
|
||||
public String getFirmwareVersion(final byte[] fwbytes) {
|
||||
final byte[] firmwareBin = getFileFromZip(fwbytes, "META/firmware.bin");
|
||||
final byte[] firmwareBin = ZipUtils.getFileFromZip(fwbytes, "META/firmware.bin");
|
||||
|
||||
if (firmwareBin == null) {
|
||||
LOG.warn("Failed to read firmware.bin");
|
||||
@ -228,7 +224,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
}
|
||||
|
||||
public String getAppName() {
|
||||
final byte[] appJsonBin = getFileFromZip(getBytes(), "app.json");
|
||||
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json");
|
||||
if (appJsonBin == null) {
|
||||
LOG.warn("Failed to get app.json from zip");
|
||||
return null;
|
||||
@ -252,7 +248,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
}
|
||||
|
||||
public String getAppType() {
|
||||
final byte[] appJsonBin = getFileFromZip(getBytes(), "app.json");
|
||||
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json");
|
||||
if (appJsonBin == null) {
|
||||
LOG.warn("Failed to get app.json from zip");
|
||||
return null;
|
||||
@ -270,35 +266,4 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] getFileFromZip(final byte[] zipBytes, final String path) {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||
if (zipEntry.getName().equals(path)) {
|
||||
return readAllBytes(zipInputStream);
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to read %s from zip", path), e);
|
||||
return null;
|
||||
}
|
||||
|
||||
LOG.debug("{} not found in zip", path);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] readAllBytes(final InputStream is) throws IOException {
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
int n;
|
||||
byte[] buf = new byte[16384];
|
||||
|
||||
while ((n = is.read(buf, 0, buf.length)) != -1) {
|
||||
buffer.write(buf, 0, n);
|
||||
}
|
||||
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class ZipUtils {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ZipUtils.class);
|
||||
|
||||
public static byte[] getFileFromZip(final byte[] zipBytes, final String path) {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||
if (zipEntry.getName().equals(path)) {
|
||||
return readAllBytes(zipInputStream);
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to read %s from zip", path), e);
|
||||
return null;
|
||||
}
|
||||
|
||||
LOG.debug("{} not found in zip", path);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] readAllBytes(final InputStream is) throws IOException {
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
int n;
|
||||
byte[] buf = new byte[16384];
|
||||
|
||||
while ((n = is.read(buf, 0, buf.length)) != -1) {
|
||||
buffer.write(buf, 0, n);
|
||||
}
|
||||
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user