anlavn.file.Zip Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AL-Library_EN Show documentation
Show all versions of AL-Library_EN Show documentation
Java library for many thing wonderful
The newest version!
package anlavn.file;
// Make By Bình An || AnLaVN || KatoVN
import anlavn.ui.DownloadPopup;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**The Zip class supports to extract a zip file to your directories.
* @author AnLaVN - https://github.com/AnLaVN
*/
public class Zip {
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[16384];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) bos.write(bytesIn, 0, read);
bos.close();
}
/**Use this method to extracts a .zip file specified to a directory specified.
* @param zipFilePath is the path of the zip file you want to extract.
* @param yourDirectory is the path of the directory containing the files after extracting.
* It will be created if it does not exist, if you leave it blank then the root directory will be specified.
*/
public static final void extract(String zipFilePath, String yourDirectory){
try{Log.add("Zip - Start collecting information.");
long totalBytesRead = 0, fileSize = 0;
int index = zipFilePath.lastIndexOf("/");
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
fileSize += entry.getSize();
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
String fileName = zipFilePath.substring(index < 0 ? 0 : index);
Log.add("Zip - Collect information successfully:\n\t\tFile name \"" + fileName + "\", total size after extract " + fileSize + " bytes.");
DownloadPopup dp = new DownloadPopup();
dp.start("Extract " + fileName);
if(yourDirectory == null || yourDirectory.equals("")) yourDirectory = new File("").getAbsolutePath();
else {
File destDir = new File(yourDirectory);
if (!destDir.exists()) destDir.mkdir();
}
zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = yourDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) extractFile(zipIn, filePath);
else new File(filePath).mkdirs();
totalBytesRead += entry.getSize();
dp.setContent(totalBytesRead + " / " + fileSize + " bytes");
dp.setValue((int) (totalBytesRead * 100 / fileSize));
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
dp.stop();
Log.add("Zip - Extract zip file successfully:\n\t\tFile name \"" + zipFilePath +"\" at directory \"" + yourDirectory + "\"");
zipIn.close();
} catch(IOException e){
Log.add("!!! Error try to extract zip file \"" + zipFilePath +"\" to \"" + yourDirectory + "\" directory. !!!\n\tError code: " + e.toString());
throw new RuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy