1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class ZipTools
{
static public final int BUFFER_SIZE = 2048;
static public final void zip(File destination, File ...sources)
throws FileNotFoundException, IOException
{
if(destination.exists())
{
destination.delete();
}
destination.createNewFile();
FileOutputStream fos = new FileOutputStream(destination);
ZipOutputStream zos = new ZipOutputStream(fos);
for(File file : sources)
{
if(file.isDirectory())
{
add(zos, file.getName() + "/", file);
}
else
{
FileInputStream fis = new FileInputStream(file);
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
byte[] buffer = new byte[BUFFER_SIZE];
int nbData;
while(fis.available() > 0)
{
nbData = fis.read(buffer);
zos.write(buffer, 0, ((nbData > BUFFER_SIZE) ? BUFFER_SIZE : nbData));
}
fis.close();
}
}
zos.close();
fos.close();
}
static public final void add(ZipOutputStream zos, String entryPath, File source)
throws FileNotFoundException, IOException
{
for(File file : source.listFiles())
{
if(file.isDirectory())
{
add(zos, entryPath + file.getName() + "/", file);
}
else
{
FileInputStream fis = new FileInputStream(file);
ZipEntry entry = new ZipEntry(entryPath + file.getName());
zos.putNextEntry(entry);
byte[] buffer = new byte[BUFFER_SIZE];
int nbData;
while((nbData = fis.available()) > 0)
{
fis.read(buffer);
zos.write(buffer, 0, ((nbData > BUFFER_SIZE) ? BUFFER_SIZE : nbData));
}
fis.close();
}
}
}
static public final void unzip(ZipFile zipfile)
throws IOException
{
File file = new File("gcp.tmp");
String path = file.getAbsolutePath();
path = path.substring(0, path.lastIndexOf(File.separatorChar));
unzip(zipfile, path);
}
static public final void unzip(ZipFile zipfile, String destinationPath)
throws IOException
{
Enumeration entries = zipfile.entries();
ZipEntry entry;
while (entries.hasMoreElements())
{
entry = (ZipEntry) entries.nextElement();
extractZipEntry(zipfile, entry, destinationPath);
}
}
static public final boolean extractZipEntry(ZipFile zipfile,
ZipEntry entry,
String destinationPath)
throws IOException
{
return extractZipEntry(zipfile, entry, destinationPath, true);
}
static public final boolean extractZipEntry(ZipFile zipfile,
ZipEntry entry,
String destinationPath,
boolean force)
throws IOException
{
String entryPath = entry.getName();
entryPath = entryPath.replace('/', File.separatorChar);
File entryFile = new File(destinationPath +
File.separatorChar +
entryPath);
if(entryFile.exists())
{
if(force)
{
entryFile.delete();
}
else
{
return false;
}
}
String fileName = entryFile.getName();
new File(entryFile.getParent()).mkdirs();
entryFile.createNewFile();
InputStream is = zipfile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(entryFile);
byte[] buffer = new byte[BUFFER_SIZE];
int nbData = 0;
while(is.available() > 0)
{
nbData = is.read(buffer);
fos.write(buffer, 0,
((nbData > BUFFER_SIZE) ? BUFFER_SIZE : nbData));
}
fos.flush();
fos.close();
is.close();
return true;
}
} |
Partager