Skip to content

Commit

Permalink
Optimized Jar Provider.
Browse files Browse the repository at this point in the history
Extractions now use the JarInputStream strategy and are significantly faster.
  • Loading branch information
dscalzi committed Jul 15, 2018
1 parent 0cbd1fd commit 6807c64
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.dscalzi</groupId>
<artifactId>ZipExtractor</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>Zip Extractor</name>
<description>Utility plugin to extract archives and compress files.</description>
<url>https://github.com/dscalzi/ZipExtractor</url>
Expand Down Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.11.2-R0.1-SNAPSHOT</version>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
62 changes: 37 additions & 25 deletions src/main/java/com/dscalzi/zipextractor/providers/JarProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
package com.dscalzi.zipextractor.providers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import org.bukkit.command.CommandSender;

import com.dscalzi.zipextractor.managers.ConfigManager;
Expand All @@ -34,22 +32,25 @@ public class JarProvider implements TypeProvider {

@Override
public List<String> scanForExtractionConflicts(CommandSender sender, File src, File dest) {

List<String> existing = new ArrayList<String>();
final MessageManager mm = MessageManager.getInstance();
mm.scanningForConflics(sender);
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

try (JarFile jar = new JarFile(src)) {
mm.scanningForConflics(sender);
Enumeration<JarEntry> enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
while (je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

JarEntry file = enumEntries.nextElement();
File newFile = new File(dest + File.separator + file.getName());
File newFile = new File(dest + File.separator + je.getName());
if (newFile.exists()) {
existing.add(file.getName());
existing.add(je.getName());
}
je = jis.getNextJarEntry();
}

jis.closeEntry();
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
Expand All @@ -65,28 +66,39 @@ public void extract(CommandSender sender, File src, File dest) {
final MessageManager mm = MessageManager.getInstance();
final Logger logger = mm.getLogger();
final boolean log = cm.getLoggingProperty();
byte[] buffer = new byte[1024];
mm.startingProcess(sender, ZTask.EXTRACT, src.getName());
try (JarFile jar = new JarFile(src)) {
Enumeration<JarEntry> enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while(je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();
JarEntry file = enumEntries.nextElement();
File f = new File(dest + File.separator + file.getName());

File newFile = new File(dest + File.separator + je.getName());
if (log)
logger.info("Extracting : " + f.getAbsolutePath());
if (file.isDirectory()) {
f.mkdir();
logger.info("Extracting : " + newFile.getAbsoluteFile());
File parent = newFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (je.isDirectory()) {
newFile.mkdir();
je = jis.getNextJarEntry();
continue;
}
try (InputStream is = jar.getInputStream(file); FileOutputStream fos = new FileOutputStream(f);) {
while (is.available() > 0)
fos.write(is.read());
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = jis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
je = jis.getNextJarEntry();
}
jis.closeEntry();
mm.extractionComplete(sender, dest.getAbsolutePath());
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.EXTRACT, e.getMessage());
} catch (FileNotFoundException e) {
mm.fileNotFound(sender, src.getAbsolutePath());
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ author: Daniel Scalzi
description: Utility plugin to extract archives and compress files.
main: com.dscalzi.zipextractor.ZipExtractor
name: ZipExtractor
version: 1.2.0
version: 1.2.1
commands:
zipextractor:
description: Accesses plugin commands.
Expand Down

0 comments on commit 6807c64

Please sign in to comment.