Skip to content

Commit

Permalink
fix: remove usage of deprecated ZipFile constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed Sep 2, 2024
1 parent 518e0a9 commit 43c0da7
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions src/main/java/net/technicpack/utilslib/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.*;
import java.nio.channels.ClosedByInterruptException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
Expand All @@ -36,10 +37,7 @@ public static boolean extractFile(File zip, File output, String fileName) throws
return false;
}

ZipFile zipFile = null;

try {
zipFile = new ZipFile(zip);
try (ZipFile zipFile = ZipFile.builder().setFile(zip).get()) {
ZipArchiveEntry entry = zipFile.getEntry(fileName);
if (entry == null) {
Utils.getLogger().log(Level.WARNING, "File " + fileName + " not found in " + zip.getAbsolutePath());
Expand All @@ -62,18 +60,14 @@ public static boolean extractFile(File zip, File output, String fileName) throws
} catch (IOException e) {
Utils.getLogger().log(Level.WARNING, "Error extracting file " + fileName + " from " + zip.getAbsolutePath());
return false;
} finally {
if (zipFile != null) {
zipFile.close();
}
}
}

private static void unzipEntry(ZipFile zipFile, ZipArchiveEntry entry, File outputFile) throws IOException, InterruptedException {
byte[] buffer = new byte[2048];

try (BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputFile.toPath()))) {
int length;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
outputStream.write(buffer, 0, length);
Expand All @@ -92,24 +86,23 @@ public static void unzipFile(File zip, File output, IZipFileFilter fileFilter, D
output.mkdirs();
}

ZipFile zipFile = new ZipFile(zip);
ArrayList<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries());
int size = entries.size();

// Commons Compress doesn't seem to throw exception when ZIP files aren't valid, so we just check if they
// have no entries as a means of validating it, and throw a ZipException so the launcher will show the correct
// warning message when trying to install/update the modpack, rather than just printing it to console.
if (size == 0) {
Utils.getLogger().log(Level.SEVERE, "Zip file is empty: " + zip.getAbsolutePath());
zipFile.close();
throw new ZipException("Zip file is empty: " + zip.getAbsolutePath());
}
try (ZipFile zipFile = ZipFile.builder().setFile(zip).get()) {
ArrayList<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries());
int size = entries.size();

// Commons Compress doesn't seem to throw exception when ZIP files aren't valid, so we just check if they
// have no entries as a means of validating it, and throw a ZipException so the launcher will show the correct
// warning message when trying to install/update the modpack, rather than just printing it to console.
if (size == 0) {
Utils.getLogger().log(Level.SEVERE, "Zip file is empty: " + zip.getAbsolutePath());
zipFile.close();
throw new ZipException("Zip file is empty: " + zip.getAbsolutePath());
}

int progress = 1;

int progress = 1;
try {
for (ZipArchiveEntry entry : entries) {
if (Thread.interrupted())
throw new InterruptedException();
if (Thread.interrupted()) throw new InterruptedException();

if (fileFilter == null || fileFilter.shouldExtract(entry.getName())) {
File outputFile = new File(output, entry.getName());
Expand Down Expand Up @@ -143,8 +136,6 @@ public static void unzipFile(File zip, File output, IZipFileFilter fileFilter, D
}
progress++;
}
} finally {
zipFile.close();
}
}
}

0 comments on commit 43c0da7

Please sign in to comment.