1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-10 23:28:25 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/ZipFile.java

116 lines
3.9 KiB
Java
Raw Normal View History

2022-09-01 15:08:02 +02:00
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.ZipException;
import java.util.zip.ZipInputStream;
import androidx.annotation.Nullable;
2022-09-02 14:28:58 +02:00
/**
* Utility class for recognition and reading of ZIP archives.
*/
public class ZipFile {
2022-09-01 15:08:02 +02:00
private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class);
public static final byte[] ZIP_HEADER = new byte[]{
0x50, 0x4B, 0x03, 0x04
};
private final byte[] zipBytes;
2022-09-01 15:08:02 +02:00
/**
2022-09-02 14:28:58 +02:00
* Open ZIP file from byte array already in memory.
* @param zipBytes data to handle as a ZIP file.
2022-09-01 15:08:02 +02:00
*/
public ZipFile(byte[] zipBytes) {
this.zipBytes = zipBytes;
2022-09-01 15:08:02 +02:00
}
2022-09-01 21:39:30 +02:00
/**
2022-09-02 14:28:58 +02:00
* Open ZIP file from InputStream.<br>
* This will read the entire file into memory at once.
* @param inputStream data to handle as a ZIP file.
2022-09-01 21:39:30 +02:00
*/
public ZipFile(InputStream inputStream) throws IOException {
this.zipBytes = readAllBytes(inputStream);
2022-09-01 21:39:30 +02:00
}
2022-09-01 15:08:02 +02:00
/**
* Checks if data resembles a ZIP file.<br>
* The check is not infallible: it may report self-extracting or other exotic ZIP archives as not a ZIP file, and it may report a corrupted ZIP file as a ZIP file.
* @param data The data to check.
* @return Whether data resembles a ZIP file.
*/
public static boolean isZipFile(byte[] data) {
return ArrayUtils.equals(data, ZIP_HEADER, 0);
}
/**
* Reads the contents of file at path into a byte array.
* @param path Path of the file in the ZIP file.
* @return byte array contatining the contents of the requested file.
* @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 {
try (InputStream is = new ByteArrayInputStream(zipBytes); ZipInputStream zipInputStream = new ZipInputStream(is)) {
2022-09-01 15:08:02 +02:00
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague.
if (zipEntry.isDirectory()) {
throw new ZipFileException(String.format("Path in ZIP file is a directory: %s", path));
}
return readAllBytes(zipInputStream);
}
throw new ZipFileException(String.format("Path in ZIP file was not found: %s", path));
} catch (ZipException e) {
throw new ZipFileException("The ZIP file might be corrupted");
} catch (IOException e) {
throw new ZipFileException("General IO error");
}
}
/**
* Tries to obtain file from ZIP file without much hassle, but is not safe.<br>
* Please only use this in place of old code where correctness of the result is checked only later on.<br>
* Use getFileFromZip of ZipFile instance instead.
* @param zipBytes
* @param path Path of the file in the ZIP file.
* @return Contents of requested file or null.
*/
@Deprecated
@Nullable
public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) {
try {
return new ZipFile(zipBytes).getFileFromZip(path);
2022-09-01 15:08:02 +02:00
} catch (ZipFileException e) {
LOG.error("Quick ZIP reading failed.", e);
} catch (Exception e) {
LOG.error("Unable to close ZipFile.", e);
}
return null;
}
private 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();
}
}