Cleanup and minor javadoc fixes
This commit is contained in:
@@ -7,39 +7,40 @@ import java.io.FileFilter;
|
||||
|
||||
/**
|
||||
* File filter for certain suffixes
|
||||
*
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class FileSuffixFilter implements FileFilter {
|
||||
|
||||
|
||||
/** Array of allowed suffixes */
|
||||
private String[] suffixes = null;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Suffix filter
|
||||
*
|
||||
*
|
||||
* @param suffixes var-args allowed suffixes, case insensitive
|
||||
*/
|
||||
public FileSuffixFilter(String... suffixes) {
|
||||
public FileSuffixFilter(String... suffixes)
|
||||
{
|
||||
this.suffixes = suffixes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean accept(File pathname)
|
||||
{
|
||||
if (!pathname.isFile()) return false;
|
||||
|
||||
|
||||
final String fname = pathname.getName().toLowerCase().trim();
|
||||
|
||||
|
||||
for (final String suffix : suffixes) {
|
||||
if (fname.endsWith(suffix.toLowerCase().trim())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,134 +16,138 @@ import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
public class FileTreeDiff {
|
||||
|
||||
|
||||
private static final byte[] BUFFER = new byte[2048];
|
||||
private final Checksum ck1 = new Adler32();
|
||||
private final Checksum ck2 = new Adler32();
|
||||
|
||||
|
||||
private boolean logging = true;
|
||||
|
||||
|
||||
private final List<Tuple<File>> compared = new ArrayList<>();
|
||||
private final Comparator<File> fileFirstSorter = new Comparator<File>() {
|
||||
|
||||
|
||||
@Override
|
||||
public int compare(File o1, File o2)
|
||||
{
|
||||
if (!o1.isDirectory() && o2.isDirectory()) return -1;
|
||||
if (o1.isDirectory() && !o2.isDirectory()) return 1;
|
||||
|
||||
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
public void enableLogging(boolean state)
|
||||
{
|
||||
logging = state;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean areEqual(File dir1, File dir2)
|
||||
{
|
||||
if (logging) Log.f3("Comparing directory trees:\n 1. " + dir1 + "\n 2. " + dir2);
|
||||
|
||||
|
||||
try {
|
||||
compared.clear();
|
||||
buildList(dir1, dir2);
|
||||
|
||||
|
||||
calcChecksum();
|
||||
|
||||
|
||||
if (logging) Log.f3("No difference found.");
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
} catch (final NotEqualException e) {
|
||||
if (logging) Log.f3("Difference found:\n" + e.getMessage());
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void calcChecksum() throws NotEqualException
|
||||
{
|
||||
|
||||
|
||||
for (final Tuple<File> pair : compared) {
|
||||
ck1.reset();
|
||||
ck2.reset();
|
||||
|
||||
try (FileInputStream in1 = new FileInputStream(pair.a); FileInputStream in2 = new FileInputStream(pair.b)) {
|
||||
|
||||
try (CheckedInputStream cin1 = new CheckedInputStream(in1, ck1); CheckedInputStream cin2 = new CheckedInputStream(in2, ck2)) {
|
||||
|
||||
|
||||
try(FileInputStream in1 = new FileInputStream(pair.a);
|
||||
FileInputStream in2 = new FileInputStream(pair.b)) {
|
||||
|
||||
try(CheckedInputStream cin1 = new CheckedInputStream(in1, ck1);
|
||||
CheckedInputStream cin2 = new CheckedInputStream(in2, ck2)) {
|
||||
|
||||
while (true) {
|
||||
final int read1 = cin1.read(BUFFER);
|
||||
final int read2 = cin2.read(BUFFER);
|
||||
|
||||
|
||||
if (read1 != read2 || ck1.getValue() != ck2.getValue()) {
|
||||
throw new NotEqualException("Bytes differ:\n" + pair.a + "\n" + pair.b);
|
||||
}
|
||||
|
||||
|
||||
if (read1 == -1) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (final IOException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void buildList(File f1, File f2) throws NotEqualException
|
||||
{
|
||||
if (f1.isDirectory() != f2.isDirectory()) throw new NotEqualException("isDirectory differs:\n" + f1 + "\n" + f2);
|
||||
|
||||
|
||||
if (f1.isFile() && f2.isFile()) {
|
||||
if (f1.length() != f2.length()) throw new NotEqualException("Sizes differ:\n" + f1 + "\n" + f2);
|
||||
}
|
||||
|
||||
|
||||
if (f1.isDirectory()) {
|
||||
final File[] children1 = f1.listFiles();
|
||||
final File[] children2 = f2.listFiles();
|
||||
|
||||
|
||||
Arrays.sort(children1, fileFirstSorter);
|
||||
Arrays.sort(children2, fileFirstSorter);
|
||||
|
||||
|
||||
if (children1.length != children2.length) throw new NotEqualException("Child counts differ:\n" + f1 + "\n" + f2);
|
||||
|
||||
|
||||
for (int i = 0; i < children1.length; i++) {
|
||||
final File ch1 = children1[i];
|
||||
final File ch2 = children2[i];
|
||||
|
||||
|
||||
if (!ch1.getName().equals(ch2.getName())) throw new NotEqualException("Filenames differ:\n" + ch1 + "\n" + ch2);
|
||||
|
||||
|
||||
buildList(ch1, ch2);
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
compared.add(new Tuple<>(f1, f2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class NotEqualException extends Exception {
|
||||
|
||||
public NotEqualException(String msg) {
|
||||
|
||||
public NotEqualException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private class Tuple<T> {
|
||||
|
||||
|
||||
public T a;
|
||||
public T b;
|
||||
|
||||
|
||||
public Tuple(T a, T b) {
|
||||
|
||||
|
||||
public Tuple(T a, T b)
|
||||
{
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
package mightypork.utils.files;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -12,9 +24,10 @@ import mightypork.utils.string.validation.StringFilter;
|
||||
|
||||
public class FileUtil {
|
||||
|
||||
|
||||
/**
|
||||
* Copy directory recursively.
|
||||
*
|
||||
*
|
||||
* @param source source file
|
||||
* @param target target file
|
||||
* @throws IOException on error
|
||||
@@ -27,7 +40,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Copy directory recursively - advanced variant.
|
||||
*
|
||||
*
|
||||
* @param source source file
|
||||
* @param target target file
|
||||
* @param filter filter accepting only files and dirs to be copied
|
||||
@@ -61,7 +74,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* List directory recursively
|
||||
*
|
||||
*
|
||||
* @param source source file
|
||||
* @param filter filter accepting only files and dirs to be copied (or null)
|
||||
* @param files list of the found files
|
||||
@@ -87,7 +100,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Copy file using streams. Make sure target directory exists!
|
||||
*
|
||||
*
|
||||
* @param source source file
|
||||
* @param target target file
|
||||
* @throws IOException on error
|
||||
@@ -95,7 +108,8 @@ public class FileUtil {
|
||||
public static void copyFile(File source, File target) throws IOException
|
||||
{
|
||||
|
||||
try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(target)) {
|
||||
try(InputStream in = new FileInputStream(source);
|
||||
OutputStream out = new FileOutputStream(target)) {
|
||||
|
||||
copyStream(in, out);
|
||||
}
|
||||
@@ -104,7 +118,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Copy bytes from input to output stream, leaving out stream open
|
||||
*
|
||||
*
|
||||
* @param in input stream
|
||||
* @param out output stream
|
||||
* @throws IOException on error
|
||||
@@ -129,7 +143,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Improved delete
|
||||
*
|
||||
*
|
||||
* @param path deleted path
|
||||
* @param recursive recursive delete
|
||||
* @return success
|
||||
@@ -153,14 +167,14 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Read entire file to a string.
|
||||
*
|
||||
*
|
||||
* @param file file
|
||||
* @return file contents
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String fileToString(File file) throws IOException
|
||||
{
|
||||
try (FileInputStream fin = new FileInputStream(file)) {
|
||||
try(FileInputStream fin = new FileInputStream(file)) {
|
||||
|
||||
return streamToString(fin);
|
||||
}
|
||||
@@ -169,7 +183,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Get files in a folder (create folder if needed)
|
||||
*
|
||||
*
|
||||
* @param dir folder
|
||||
* @return list of files
|
||||
*/
|
||||
@@ -181,7 +195,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Get files in a folder (create folder if needed)
|
||||
*
|
||||
*
|
||||
* @param dir folder
|
||||
* @param filter file filter
|
||||
* @return list of files
|
||||
@@ -202,7 +216,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Remove extension.
|
||||
*
|
||||
*
|
||||
* @param file file
|
||||
* @return filename without extension
|
||||
*/
|
||||
@@ -226,7 +240,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Remove extension.
|
||||
*
|
||||
*
|
||||
* @param filename
|
||||
* @return filename and extension
|
||||
*/
|
||||
@@ -253,7 +267,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Read entire input stream to a string, and close it.
|
||||
*
|
||||
*
|
||||
* @param in input stream
|
||||
* @return file contents
|
||||
*/
|
||||
@@ -265,7 +279,7 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Read input stream to a string, and close it.
|
||||
*
|
||||
*
|
||||
* @param in input stream
|
||||
* @param lines max number of lines (-1 to disable limit)
|
||||
* @return file contents
|
||||
@@ -327,9 +341,8 @@ public class FileUtil {
|
||||
if (in != null) return in;
|
||||
|
||||
try {
|
||||
return new FileInputStream(new File(".", path));
|
||||
return new FileInputStream(WorkDir.getFile(path));
|
||||
} catch (final FileNotFoundException e) {
|
||||
// error
|
||||
Log.w("Could not open resource stream: " + path);
|
||||
return null;
|
||||
}
|
||||
@@ -339,20 +352,20 @@ public class FileUtil {
|
||||
|
||||
public static String getResourceAsString(String path)
|
||||
{
|
||||
return streamToString(FileUtil.class.getResourceAsStream(path));
|
||||
return streamToString(getResource(path));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save string to file
|
||||
*
|
||||
*
|
||||
* @param file file
|
||||
* @param text string
|
||||
* @throws IOException on error
|
||||
*/
|
||||
public static void stringToFile(File file, String text) throws IOException
|
||||
{
|
||||
try (PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")) {
|
||||
try(PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")) {
|
||||
|
||||
out.print(text);
|
||||
|
||||
@@ -393,14 +406,15 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Copy resource to file
|
||||
*
|
||||
*
|
||||
* @param resname resource name
|
||||
* @param file out file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void resourceToFile(String resname, File file) throws IOException
|
||||
{
|
||||
try (InputStream in = FileUtil.getResource(resname); OutputStream out = new FileOutputStream(file)) {
|
||||
try(InputStream in = FileUtil.getResource(resname);
|
||||
OutputStream out = new FileOutputStream(file)) {
|
||||
|
||||
FileUtil.copyStream(in, out);
|
||||
}
|
||||
@@ -410,14 +424,14 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* Get resource as string, safely closing streams.
|
||||
*
|
||||
*
|
||||
* @param resname resource name
|
||||
* @return resource as string, empty string on failure
|
||||
* @throws IOException on fail
|
||||
*/
|
||||
public static String resourceToString(String resname) throws IOException
|
||||
{
|
||||
try (InputStream in = FileUtil.getResource(resname)) {
|
||||
try(InputStream in = FileUtil.getResource(resname)) {
|
||||
return streamToString(in);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,23 +9,23 @@ import java.nio.channels.FileLock;
|
||||
|
||||
/**
|
||||
* Instance lock (avoid running twice)
|
||||
*
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class InstanceLock {
|
||||
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public static boolean onFile(final File lockFile)
|
||||
{
|
||||
try {
|
||||
lockFile.getParentFile().mkdirs();
|
||||
final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw");
|
||||
|
||||
|
||||
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
|
||||
if (fileLock != null) {
|
||||
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
@@ -39,10 +39,10 @@ public class InstanceLock {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
} catch (final IOException e) {
|
||||
System.err.println("IO error while obtaining lock.");
|
||||
@@ -50,5 +50,5 @@ public class InstanceLock {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,90 +5,90 @@ import java.io.File;
|
||||
|
||||
|
||||
public class OsUtils {
|
||||
|
||||
|
||||
public static enum EnumOS
|
||||
{
|
||||
linux, macos, solaris, unknown, windows;
|
||||
|
||||
|
||||
public boolean isLinux()
|
||||
{
|
||||
return this == linux || this == solaris;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isMac()
|
||||
{
|
||||
return this == macos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isWindows()
|
||||
{
|
||||
return this == windows;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static EnumOS cachedOs;
|
||||
|
||||
|
||||
|
||||
|
||||
public static File getHomeWorkDir(String dirname)
|
||||
{
|
||||
final String userhome = System.getProperty("user.home", ".");
|
||||
File file;
|
||||
|
||||
|
||||
switch (getOs()) {
|
||||
case linux:
|
||||
case solaris:
|
||||
file = new File(userhome, dirname + '/');
|
||||
break;
|
||||
|
||||
|
||||
case windows:
|
||||
final String appdata = System.getenv("APPDATA");
|
||||
|
||||
|
||||
if (appdata != null) {
|
||||
file = new File(appdata, dirname + '/');
|
||||
} else {
|
||||
file = new File(userhome, dirname + '/');
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case macos:
|
||||
file = new File(userhome, "Library/Application Support/" + dirname);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
file = new File(userhome, dirname + "/");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static EnumOS getOs()
|
||||
{
|
||||
if (cachedOs != null) return cachedOs;
|
||||
|
||||
|
||||
final String s = System.getProperty("os.name").toLowerCase();
|
||||
|
||||
|
||||
if (s.contains("win")) {
|
||||
cachedOs = EnumOS.windows;
|
||||
|
||||
|
||||
} else if (s.contains("mac")) {
|
||||
cachedOs = EnumOS.macos;
|
||||
|
||||
|
||||
} else if (s.contains("linux") || s.contains("unix")) {
|
||||
cachedOs = EnumOS.linux;
|
||||
|
||||
|
||||
} else if (s.contains("solaris") || s.contains("sunos")) {
|
||||
cachedOs = EnumOS.solaris;
|
||||
|
||||
|
||||
} else {
|
||||
cachedOs = EnumOS.unknown;
|
||||
}
|
||||
|
||||
|
||||
return cachedOs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package mightypork.utils.files;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
/**
|
||||
* Working directory helper.
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class WorkDir {
|
||||
|
||||
private static File baseDir = new File(".");
|
||||
private static Map<String, String> namedPaths = new HashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the workdir for the given root path
|
||||
*
|
||||
* @param workdir workdir root path
|
||||
*/
|
||||
public static void setBaseDir(File workdir)
|
||||
{
|
||||
WorkDir.baseDir = workdir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a path alias (dir or file), relative to the workdir.
|
||||
*
|
||||
* @param alias path alias
|
||||
* @param path path relative to workdir
|
||||
*/
|
||||
public static void addPath(String alias, String path)
|
||||
{
|
||||
namedPaths.put(alias, path);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get workdir folder, create if not exists.
|
||||
*
|
||||
* @param path dir path relative to workdir
|
||||
* @return dir file
|
||||
*/
|
||||
public static File getDir(String path)
|
||||
{
|
||||
if (namedPaths.containsKey(path)) path = namedPaths.get(path);
|
||||
|
||||
final File f = new File(baseDir, path);
|
||||
if (!f.exists()) {
|
||||
if (!f.mkdirs()) {
|
||||
Log.w("Could not create a directory: " + f + " (path: " + path + ")");
|
||||
}
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get workdir file, create parent if not exists.
|
||||
*
|
||||
* @param path dir path relative to workdir
|
||||
* @return dir file
|
||||
*/
|
||||
public static File getFile(String path)
|
||||
{
|
||||
if (namedPaths.containsKey(path)) path = namedPaths.get(path);
|
||||
|
||||
final File f = new File(baseDir, path);
|
||||
|
||||
// create the parent dir
|
||||
if (!f.getParent().equals(baseDir)) {
|
||||
f.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
return f;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the workdir File
|
||||
*/
|
||||
public static File getBaseDir()
|
||||
{
|
||||
return baseDir;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,31 +16,32 @@ import mightypork.utils.logging.Log;
|
||||
|
||||
/**
|
||||
* Class for building a zip file
|
||||
*
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class ZipBuilder {
|
||||
|
||||
|
||||
private final ZipOutputStream out;
|
||||
private final HashSet<String> included = new HashSet<>();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param target target zip file
|
||||
* @throws IOException if the file is directory or cannot be created
|
||||
*/
|
||||
public ZipBuilder(File target) throws IOException {
|
||||
|
||||
public ZipBuilder(File target) throws IOException
|
||||
{
|
||||
|
||||
if (!target.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
|
||||
|
||||
|
||||
final FileOutputStream dest = new FileOutputStream(target);
|
||||
out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add stream to a path
|
||||
*
|
||||
*
|
||||
* @param path path
|
||||
* @param in stream
|
||||
* @throws IOException
|
||||
@@ -53,16 +54,16 @@ public class ZipBuilder {
|
||||
return; // ignore
|
||||
}
|
||||
included.add(path);
|
||||
|
||||
|
||||
out.putNextEntry(new ZipEntry(path));
|
||||
|
||||
|
||||
FileUtil.copyStream(in, out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add string as a file
|
||||
*
|
||||
*
|
||||
* @param path path
|
||||
* @param text text to write
|
||||
* @throws IOException
|
||||
@@ -72,18 +73,18 @@ public class ZipBuilder {
|
||||
path = preparePath(path);
|
||||
if (included.contains(path)) return; // ignore
|
||||
included.add(path);
|
||||
|
||||
|
||||
out.putNextEntry(new ZipEntry(path));
|
||||
|
||||
try (InputStream in = FileUtil.stringToStream(text)) {
|
||||
|
||||
try(InputStream in = FileUtil.stringToStream(text)) {
|
||||
FileUtil.copyStream(in, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add resource obtained via FileUtils.getResource()
|
||||
*
|
||||
*
|
||||
* @param path path
|
||||
* @param resPath resource path
|
||||
* @throws IOException
|
||||
@@ -93,34 +94,34 @@ public class ZipBuilder {
|
||||
path = preparePath(path);
|
||||
if (included.contains(path)) return; // ignore
|
||||
included.add(path);
|
||||
|
||||
|
||||
out.putNextEntry(new ZipEntry(path));
|
||||
|
||||
try (InputStream in = FileUtil.getResource(resPath)) {
|
||||
|
||||
try(InputStream in = FileUtil.getResource(resPath)) {
|
||||
FileUtil.copyStream(in, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Normalize path
|
||||
*
|
||||
*
|
||||
* @param path original path
|
||||
* @return normalized path
|
||||
*/
|
||||
private static String preparePath(String path)
|
||||
{
|
||||
path = path.replace("\\", "/");
|
||||
|
||||
|
||||
if (path.charAt(0) == '/') path = path.substring(1);
|
||||
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Close the zip stream
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void close() throws IOException
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package mightypork.utils.files.zip;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
@@ -15,17 +20,17 @@ import mightypork.utils.string.validation.StringFilter;
|
||||
|
||||
/**
|
||||
* Utilities for manipulating zip files
|
||||
*
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class ZipUtils {
|
||||
|
||||
|
||||
private static final int BUFFER_SIZE = 2048;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Extract zip file to target directory
|
||||
*
|
||||
*
|
||||
* @param file zip file
|
||||
* @param outputDir target directory
|
||||
* @param filter string filter (will be used to test entry names (paths))
|
||||
@@ -34,15 +39,15 @@ public class ZipUtils {
|
||||
*/
|
||||
public static List<String> extractZip(File file, File outputDir, StringFilter filter) throws IOException
|
||||
{
|
||||
try (ZipFile zip = new ZipFile(file)) {
|
||||
try(ZipFile zip = new ZipFile(file)) {
|
||||
return extractZip(zip, outputDir, filter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Extract zip file to target directory
|
||||
*
|
||||
*
|
||||
* @param zip open zip file
|
||||
* @param outputDir target directory
|
||||
* @param filter string filter (will be used to test entry names (paths))
|
||||
@@ -52,53 +57,53 @@ public class ZipUtils {
|
||||
public static List<String> extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException
|
||||
{
|
||||
final ArrayList<String> files = new ArrayList<>();
|
||||
|
||||
|
||||
if (!outputDir.mkdirs()) throw new IOException("Could not create output directory.");
|
||||
|
||||
|
||||
final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
|
||||
|
||||
|
||||
// process each entry
|
||||
while (zipFileEntries.hasMoreElements()) {
|
||||
final ZipEntry entry = zipFileEntries.nextElement();
|
||||
|
||||
|
||||
// parse filename and path
|
||||
final String entryPath = entry.getName();
|
||||
final File destFile = new File(outputDir, entryPath);
|
||||
final File destinationParent = destFile.getParentFile();
|
||||
|
||||
|
||||
if (entry.isDirectory() || (filter != null && !filter.isValid(entryPath))) continue;
|
||||
|
||||
|
||||
// make sure directories exist
|
||||
if (!destinationParent.mkdirs()) throw new IOException("Could not create directory.");
|
||||
|
||||
|
||||
if (!entry.isDirectory()) {
|
||||
extractZipEntry(zip, entry, destFile);
|
||||
files.add(entryPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Read zip entries and add their paths to a list
|
||||
*
|
||||
*
|
||||
* @param zipFile open zip file
|
||||
* @return list of entry names
|
||||
* @throws IOException on error
|
||||
*/
|
||||
public static List<String> listZip(File zipFile) throws IOException
|
||||
{
|
||||
try (ZipFile zip = new ZipFile(zipFile)) {
|
||||
try(ZipFile zip = new ZipFile(zipFile)) {
|
||||
return listZip(zip);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Read zip entries and add their paths to a list
|
||||
*
|
||||
*
|
||||
* @param zip open zip file
|
||||
* @return list of entry names
|
||||
* @throws IOException on error
|
||||
@@ -106,25 +111,25 @@ public class ZipUtils {
|
||||
public static List<String> listZip(ZipFile zip) throws IOException
|
||||
{
|
||||
final ArrayList<String> files = new ArrayList<>();
|
||||
|
||||
|
||||
final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
|
||||
|
||||
|
||||
// process each entry
|
||||
while (zipFileEntries.hasMoreElements()) {
|
||||
final ZipEntry entry = zipFileEntries.nextElement();
|
||||
|
||||
|
||||
if (!entry.isDirectory()) {
|
||||
files.add(entry.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Extract one zip entry to target file
|
||||
*
|
||||
*
|
||||
* @param zip open zip file
|
||||
* @param entry entry from the zip file
|
||||
* @param destFile destination file ((NOT directory!)
|
||||
@@ -133,17 +138,20 @@ public class ZipUtils {
|
||||
public static void extractZipEntry(ZipFile zip, ZipEntry entry, File destFile) throws IOException
|
||||
{
|
||||
if (!destFile.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
|
||||
|
||||
try (InputStream in = zip.getInputStream(entry); BufferedInputStream is = new BufferedInputStream(in); FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
|
||||
|
||||
|
||||
try(InputStream in = zip.getInputStream(entry);
|
||||
BufferedInputStream is = new BufferedInputStream(in);
|
||||
FileOutputStream fos = new FileOutputStream(destFile);
|
||||
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
|
||||
|
||||
FileUtil.copyStream(is, dest);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Load zip entry to String
|
||||
*
|
||||
*
|
||||
* @param zip open zip file
|
||||
* @param entry entry from the zip file
|
||||
* @return loaded string
|
||||
@@ -164,16 +172,16 @@ public class ZipUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static boolean entryExists(File selectedFile, String string)
|
||||
{
|
||||
try (ZipFile zf = new ZipFile(selectedFile)) {
|
||||
try(ZipFile zf = new ZipFile(selectedFile)) {
|
||||
return zf.getEntry(string) != null;
|
||||
} catch (final IOException | RuntimeException e) {
|
||||
Log.w("Error reading zip.", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user