|
1 | 1 | package software.coley.llzip;
|
2 | 2 |
|
| 3 | +import software.coley.llzip.format.model.CentralDirectoryFileHeader; |
| 4 | +import software.coley.llzip.format.model.EndOfCentralDirectory; |
| 5 | +import software.coley.llzip.format.model.ZipArchive; |
3 | 6 | import software.coley.llzip.format.read.ForwardScanZipReaderStrategy;
|
4 | 7 | import software.coley.llzip.format.read.JvmZipReaderStrategy;
|
| 8 | +import software.coley.llzip.format.read.NaiveLocalFileZipReaderStrategy; |
5 | 9 | import software.coley.llzip.format.read.ZipReaderStrategy;
|
6 |
| -import software.coley.llzip.format.model.ZipArchive; |
7 | 10 | import software.coley.llzip.util.BufferData;
|
8 | 11 | import software.coley.llzip.util.ByteData;
|
9 | 12 | import software.coley.llzip.util.FileMapUtil;
|
|
15 | 18 |
|
16 | 19 | /**
|
17 | 20 | * IO wrappers for reading {@link ZipArchive} contents.
|
| 21 | + * <ul> |
| 22 | + * <li>For JAR files or anything intended to be read by the JVM use the JVM operations which use {@link JvmZipReaderStrategy}.</li> |
| 23 | + * <li>For regular ZIP files use {@link ForwardScanZipReaderStrategy}.</li> |
| 24 | + * <li>For ZIP files without {@link CentralDirectoryFileHeader} or {@link EndOfCentralDirectory} items, use {@link NaiveLocalFileZipReaderStrategy}</li> |
| 25 | + * </ul> |
18 | 26 | *
|
19 | 27 | * @author Matt Coley
|
20 | 28 | */
|
@@ -64,6 +72,51 @@ public static ZipArchive readStandard(Path data) throws IOException {
|
64 | 72 | return read(data, new ForwardScanZipReaderStrategy());
|
65 | 73 | }
|
66 | 74 |
|
| 75 | + /** |
| 76 | + * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy}. |
| 77 | + * |
| 78 | + * @param data |
| 79 | + * Zip bytes. |
| 80 | + * |
| 81 | + * @return Archive from bytes. |
| 82 | + * |
| 83 | + * @throws IOException |
| 84 | + * When the archive bytes cannot be read from, usually indicating a malformed zip. |
| 85 | + */ |
| 86 | + public static ZipArchive readNaive(ByteData data) throws IOException { |
| 87 | + return read(data, new NaiveLocalFileZipReaderStrategy()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy}. |
| 92 | + * |
| 93 | + * @param data |
| 94 | + * Zip bytes. |
| 95 | + * |
| 96 | + * @return Archive from bytes. |
| 97 | + * |
| 98 | + * @throws IOException |
| 99 | + * When the archive bytes cannot be read from, usually indicating a malformed zip. |
| 100 | + */ |
| 101 | + public static ZipArchive readNaive(byte[] data) throws IOException { |
| 102 | + return read(data, new NaiveLocalFileZipReaderStrategy()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy}. |
| 107 | + * |
| 108 | + * @param data |
| 109 | + * Zip path. |
| 110 | + * |
| 111 | + * @return Archive from bytes. |
| 112 | + * |
| 113 | + * @throws IOException |
| 114 | + * When the archive bytes cannot be read from, usually indicating a malformed zip. |
| 115 | + */ |
| 116 | + public static ZipArchive readNaive(Path data) throws IOException { |
| 117 | + return read(data, new NaiveLocalFileZipReaderStrategy()); |
| 118 | + } |
| 119 | + |
67 | 120 | /**
|
68 | 121 | * Creates an archive using the {@link JvmZipReaderStrategy} which handles some edge cases not usually
|
69 | 122 | * expected from zip files.
|
@@ -127,6 +180,7 @@ public static ZipArchive read(ByteData data, ZipReaderStrategy strategy) throws
|
127 | 180 | if (data == null)
|
128 | 181 | throw new IOException("Data is null!");
|
129 | 182 | // The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
|
| 183 | + // - Even if we only want to read local/central file entries, those are even larger at a minimum |
130 | 184 | if (data.length() < 22)
|
131 | 185 | throw new IOException("Not enough bytes to read Central-Directory-File-Header, minimum=22");
|
132 | 186 | // Create instance
|
|
0 commit comments