Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
adding zip util to selectively unzip files
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jan 15, 2019
1 parent 7762e9c commit 9b75bda
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Add to your `pom.xml`
<dependency>
<groupId>me.shib.java.lib</groupId>
<artifactId>utils</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</dependency>
```
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>me.shib.java.lib</groupId>
<artifactId>utils</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<name>Utils</name>
<description>Commonly used custom utils</description>
<url>https://github.com/shibme/utils</url>
Expand Down Expand Up @@ -75,19 +75,6 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
44 changes: 44 additions & 0 deletions src/me/shib/java/lib/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down Expand Up @@ -43,6 +45,48 @@ public static boolean unZip(File zipFile, File outputDirectory) {
}
}

public static boolean unZip(File zipFile, String fileToExtract, File outputDirectory) {
List<String> filesToExtract = new ArrayList<>();
filesToExtract.add(fileToExtract);
return unZip(zipFile, filesToExtract, outputDirectory);
}

public static boolean unZip(File zipFile, List<String> filesToExtract, File outputDirectory) {
byte[] buffer = new byte[1024];
try {
if (!outputDirectory.exists()) {
outputDirectory.mkdir();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(outputDirectory.getAbsolutePath() + File.separator + fileName);
if (filesToExtract.contains(newFile.getName())) {
System.out.println("Extracting : " + newFile.getAbsoluteFile());
if (ze.isDirectory()) {
newFile.mkdirs();
} else {
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
}
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Extracted " + zipFile.getName() + " successfully!");
return true;
} catch (IOException ex) {
return false;
}
}

private static String calculateChecksum(File file, String hashType) {
InputStream is = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/me/shib/java/lib/utils/LocalFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String[] getKeys(String type) {
if (keyDir.exists()) {
String[] encodedKeys = keyDir.list();
ArrayList<String> keyList = new ArrayList<>();
if(encodedKeys != null) {
if (encodedKeys != null) {
for (String enKey : encodedKeys) {
String key = decodeKeyToName(enKey.replace(".json", ""));
if (key != null) {
Expand Down

0 comments on commit 9b75bda

Please sign in to comment.