public static void main(String... strings) throws Exception {
File[] sourceFiles = { new File("D:\\download\\dbf\\BMP\\KEY.BMP"),//
new File("D:\\download\\favicon.ico"), //
new File("D:\\download\\SDelete.zip"), //
new File("D:\\download\\iepngfix\\") //
};
File rootDir = new File("D:\\download\\");
File zipFile = new File(rootDir, "download.zip");
ZipUtils.zip(rootDir, sourceFiles, zipFile, Deflater.DEFAULT_COMPRESSION);
for (String s : ZipUtils.unzip(zipFile)) {
System.out.println(s);
}
}
output
dbf/BMP/KEY.BMP
favicon.ico
SDelete.zip
iepngfix/.htaccess
iepngfix/blank.gif
iepngfix/checkerboard.gif
iepngfix/helix.gif
iepngfix/iepngfix.htc
iepngfix/iepngfix.html
iepngfix/iepngfix.php
iepngfix/iepngfix_tilebg.js
iepngfix/opacity.png
iepngfix/opacity2.png
iepngfix/shadow.png
ru.olimpiks.common.utils.ZipUtils.java
package ru.olimpiks.common.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BATCH_SIZE = 4096;
private static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte buffer[] = new byte[BATCH_SIZE];
long count = 0L;
for (int n = 0; -1 != (n = input.read(buffer));) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
private static final void closeQuietly(Closeable zos) {
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
}
}
private static String normalizeEntryName(String entryName) {
return entryName.replace(File.separatorChar, '/');
}
private static String getEntryName(File rootDir, File zippedFile) {
if (rootDir != null && zippedFile.getPath().startsWith(rootDir.getPath())) {
return zippedFile.getPath().substring(rootDir.getPath().length() + 1);
} else {
return zippedFile.getPath();
}
}
private static void zipFile(File zippedFile, String entryName,
ZipOutputStream zos) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zippedFile);
ZipEntry anEntry = new ZipEntry(normalizeEntryName(entryName));
zos.putNextEntry(anEntry);
copyLarge(fis, zos);
fis.close();
zos.flush();
}
private static void zipDir(File zippedDir, String entryName,
ZipOutputStream zos) throws FileNotFoundException, IOException {
File[] dirList = zippedDir.listFiles();
for (File f : dirList) {
if (f.isDirectory()) {
zipDir(f, entryName + "/" + f.getName(), zos);
continue;
}
zipFile(f, entryName + "/" + f.getName(), zos);
}
}
public static final void zipFile(File rootDir, File zippedFile, File zipFile,
int level) throws FileNotFoundException, IOException {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile)));
zos.setLevel(level);
zipFile(zippedFile, getEntryName(rootDir, zippedFile), zos);
} finally {
closeQuietly(zos);
}
}
public static final void zipDir(File rootDir, File zippedDir, File zipFile,
int level) throws FileNotFoundException, IOException {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile)));
zos.setLevel(level);
zipDir(zippedDir, getEntryName(rootDir, zippedDir), zos);
} finally {
closeQuietly(zos);
}
}
public static final void zip(File rootDir, File[] zipped, File zipFile,
int level) throws FileNotFoundException, IOException {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile)));
zos.setLevel(level);
for (File f : zipped) {
if (f.isDirectory()) {
zipDir(f, getEntryName(rootDir, f), zos);
} else {
zipFile(f, getEntryName(rootDir, f), zos);
}
}
} finally {
closeQuietly(zos);
}
}
public static final void unzip(File zipFile, File outDir)
throws FileNotFoundException, IOException {
ZipInputStream in = null;
try {
in = new ZipInputStream(new BufferedInputStream(new FileInputStream(
zipFile)));
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
File file = new File(outDir, entry.getName());
System.out.println(file);
if (!file.exists()) {
if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
throw new IOException("Could not create directory : "
+ file.getParentFile());
}
if (!file.createNewFile()) {
throw new IOException("Could not create file : "
+ file.getParentFile());
}
}
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
copyLarge(in, out);
out.flush();
out.close();
}
} finally {
closeQuietly(in);
}
}
public static final String[] unzip(File zipFile)
throws FileNotFoundException, IOException {
List<String> list = new ArrayList<String>(0);
ZipInputStream in = null;
try {
in = new ZipInputStream(new BufferedInputStream(new FileInputStream(
zipFile)));
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
list.add(entry.getName());
}
return list.toArray(new String[0]);
} finally {
closeQuietly(in);
}
}
}

0 comments:
Отправить комментарий