2014-07-06 07:58:44 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-07-06 07:58:44 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FilenameFilter;
|
2015-02-02 06:15:16 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-07-06 07:58:44 +02:00
|
|
|
|
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
|
|
import net.lingala.zip4j.exception.ZipException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpacker for OSZ (ZIP) archives.
|
|
|
|
*/
|
|
|
|
public class OszUnpacker {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The index of the current file being unpacked. */
|
2014-07-06 07:58:44 +02:00
|
|
|
private static int fileIndex = -1;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The total number of directories to parse. */
|
2014-07-06 07:58:44 +02:00
|
|
|
private static File[] files;
|
|
|
|
|
|
|
|
// This class should not be instantiated.
|
|
|
|
private OszUnpacker() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes the unpacker for each OSZ archive in a root directory.
|
|
|
|
* @param root the root directory
|
|
|
|
* @param dest the destination directory
|
2015-02-02 06:15:16 +01:00
|
|
|
* @return an array containing the new (unpacked) directories, or null
|
|
|
|
* if no OSZs found
|
2014-07-06 07:58:44 +02:00
|
|
|
*/
|
2015-02-02 06:15:16 +01:00
|
|
|
public static File[] unpackAllFiles(File root, File dest) {
|
|
|
|
List<File> dirs = new ArrayList<File>();
|
|
|
|
|
|
|
|
// find all OSZ files
|
2014-07-06 07:58:44 +02:00
|
|
|
files = root.listFiles(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
return name.toLowerCase().endsWith(".osz");
|
|
|
|
}
|
|
|
|
});
|
2015-02-12 08:27:33 +01:00
|
|
|
if (files == null || files.length < 1) {
|
2014-07-06 07:58:44 +02:00
|
|
|
files = null;
|
2015-02-02 06:15:16 +01:00
|
|
|
return new File[0];
|
2014-07-06 07:58:44 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
// unpack OSZs
|
2014-07-06 07:58:44 +02:00
|
|
|
for (File file : files) {
|
|
|
|
fileIndex++;
|
|
|
|
String dirName = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
|
|
|
File songDir = new File(dest, dirName);
|
|
|
|
if (!songDir.isDirectory()) {
|
|
|
|
songDir.mkdir();
|
|
|
|
unzip(file, songDir);
|
2015-01-21 01:01:18 +01:00
|
|
|
file.delete(); // delete the OSZ when finished
|
2015-02-02 06:15:16 +01:00
|
|
|
dirs.add(songDir);
|
2014-07-06 07:58:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileIndex = -1;
|
|
|
|
files = null;
|
2015-02-02 06:15:16 +01:00
|
|
|
return dirs.toArray(new File[dirs.size()]);
|
2014-07-06 07:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the contents of a ZIP archive to a destination.
|
|
|
|
* @param file the ZIP archive
|
|
|
|
* @param dest the destination directory
|
|
|
|
*/
|
|
|
|
private static void unzip(File file, File dest) {
|
2014-12-20 21:30:20 +01:00
|
|
|
try {
|
|
|
|
ZipFile zipFile = new ZipFile(file);
|
|
|
|
zipFile.extractAll(dest.getAbsolutePath());
|
|
|
|
} catch (ZipException e) {
|
2015-01-16 03:55:26 +01:00
|
|
|
ErrorHandler.error(String.format("Failed to unzip file %s to dest %s.",
|
|
|
|
file.getAbsolutePath(), dest.getAbsolutePath()), e, false);
|
2014-12-20 21:30:20 +01:00
|
|
|
}
|
2014-07-06 07:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the current file being unpacked, or null if none.
|
|
|
|
*/
|
|
|
|
public static String getCurrentFileName() {
|
|
|
|
if (files == null || fileIndex == -1)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return files[fileIndex].getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the progress of file unpacking, or -1 if not unpacking.
|
|
|
|
* @return the completion percent [0, 100] or -1
|
|
|
|
*/
|
|
|
|
public static int getUnpackerProgress() {
|
|
|
|
if (files == null || fileIndex == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return (fileIndex + 1) * 100 / files.length;
|
|
|
|
}
|
|
|
|
}
|