package co.wlv.apputil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
public class CalculateFileSizeInDir {
public static void main(String args[]) throws Exception {
String zipFileName = "C:\\path\\to\\my.zip";
File initialFile = new File(zipFileName);
InputStream targetStream = new FileInputStream(initialFile);
File extractDir = new File("output");
FileUtils.deleteDirectory(extractDir);
unZipImportData(targetStream, extractDir);
List files = (List) FileUtils.listFiles(extractDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
System.out.println("file: " + file.getCanonicalPath() + " - file Size: " + readableFileSize(file.length()));
}
}
public static String readableFileSize(long size) {
if (size <= 0)
return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
public static double fileSize(long size) {
if (size <= 0)
return 0;
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return size / Math.pow(1024, digitGroups);
}
public static String getUnits(long size) {
if (size <= 0)
return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return units[digitGroups];
}
public static void unZipImportData(InputStream zip, File extractTo) throws Exception {
ZipInputStream zipInputStream = new ZipInputStream(zip);
ZipEntry zipEntry;
try {
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
File file = new File(extractTo, zipEntry.getName());
if (zipEntry.isDirectory() && !file.exists())
file.mkdirs();
else {
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[8192];
int read;
while (-1 != (read = zipInputStream.read(buffer)))
out.write(buffer, 0, read);
zipInputStream.closeEntry();
out.close();
}
}
zipInputStream.close();
zipInputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
Export this post to PDF document