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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| package zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* @author *
*/
public class ZIP {
private static final int DEFAULT_LEVEL_COMPRESSION = Deflater.BEST_COMPRESSION;
private static final String ZIP_EXTENSION = ".zip";
public static byte[] calculerTableauByt(File zipFile, String nomFichierRecherche) throws Exception {
byte[] tableauOctet = null;
// Ouvre le zip avec le buffer
final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile.getCanonicalFile())));
ZipEntry zipEntree;
int count;
int nombreFichier = 0;
// Parcourt tous les fichiers
while ((zipEntree = zipStream.getNextEntry()) != null) {
int dernierSlash = zipEntree.getName().lastIndexOf("/") + 1;
String nomFichier = zipEntree.getName().substring(dernierSlash);
nombreFichier++;
if (!nomFichier.equals(nomFichierRecherche)) {
continue;
}
int TAILLE_BUFFER = 1024;
byte data[] = new byte[TAILLE_BUFFER];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// Ecrit le fichier
while ((count = zipStream.read(data, 0, TAILLE_BUFFER)) != -1) {
byteArrayOutputStream.write(data, 0, count);
}
tableauOctet = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
}
return tableauOctet;
}
public static void compress(final File file) throws IOException {
compress(file, file, DEFAULT_LEVEL_COMPRESSION);
}
public static void compress(final File file, final File target) throws IOException {
compress(file, target, DEFAULT_LEVEL_COMPRESSION);
}
// Compresse un fichier à l'adresse pointée par le fichier cible.
// Remplace le fichier cible s'il existe déjà.
public static void compress(final File file, final File target, final int compressionLevel) throws IOException {
final File source = file.getCanonicalFile();
// Création du fichier zip
final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(getZipTypeFile(source, target.getCanonicalFile())));
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(compressionLevel);
// Ajout du(es) fichier(s) au zip
compressFile(out, "", source);
out.close();
}
public static void compress(final File file, final int compressionLevel) throws IOException {
compress(file, file, compressionLevel);
}
public static void compress(final String fileName) throws IOException {
compress(new File(fileName), new File(fileName), DEFAULT_LEVEL_COMPRESSION);
}
public static void compress(final String fileName, final int compressionLevel) throws IOException {
compress(new File(fileName), new File(fileName), compressionLevel);
}
public static void compress(final String fileName, final String targetName) throws IOException {
compress(new File(fileName), new File(targetName), DEFAULT_LEVEL_COMPRESSION);
}
public static void compress(final String fileName, final String targetName, final int compressionLevel) throws IOException {
compress(new File(fileName), new File(targetName), compressionLevel);
}
// Compresse un fichier
private final static void compressFile(final ZipOutputStream out, final String parentFolder, final File file) throws IOException {
final String zipName = new StringBuffer(parentFolder).append(file.getName()).append(file.isDirectory() ? "/" : "").toString();
// Définition des attributs du fichier
final ZipEntry entry = new ZipEntry(zipName);
entry.setSize(file.length());
entry.setTime(file.lastModified());
out.putNextEntry(entry);
// Traitement récursif s'il s'agit d'un répertoire
if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
compressFile(out, zipName.toString(), files[i]);
}
return;
}
// Ecriture du fichier dans le zip
final InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
final byte[] buf = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = in.read(buf))) {
out.write(buf, 0, bytesRead);
}
} finally {
in.close();
}
}
public static void decompress(final File file) throws IOException {
decompress(file, file.getCanonicalFile().getParentFile(), false);
}
public static void decompress(final File file, final boolean deleteZipAfter) throws IOException {
decompress(file, file.getCanonicalFile().getParentFile(), deleteZipAfter);
}
// Décompresse un fichier zip à l'adresse indiquée par le dossier
public static void decompress(final File file, final File folder, final boolean deleteZipAfter) throws IOException {
final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file.getCanonicalFile())));
ZipEntry ze;
try {
// Parcourt tous les fichiers
while (null != (ze = zis.getNextEntry())) {
final File f = new File(folder.getCanonicalPath(), ze.getName());
System.out.println("file " + f.getName());
if (f.exists()) {
f.delete();
}
// Création des dossiers
if (ze.isDirectory()) {
f.mkdirs();
continue;
}
f.getParentFile().mkdirs();
final OutputStream fos = new BufferedOutputStream(new FileOutputStream(f));
//
// Ecriture des fichiers
try {
try {
final byte[] buf = new byte[108192];
int bytesRead;
while (-1 != (bytesRead = zis.read(buf))) {
fos.write(buf, 0, bytesRead);
}
} finally {
fos.close();
}
} catch (final IOException ioe) {
f.delete();
throw ioe;
}
}
} finally {
zis.close();
}
if (deleteZipAfter) {
file.delete();
}
}
public static void decompress(final String fileName) throws IOException {
decompress(new File(fileName));
}
public static void decompress(final String fileName, final boolean deleteZipAfter) throws IOException {
decompress(new File(fileName), deleteZipAfter);
}
public static void decompress(final String fileName, final String folderName) throws IOException {
decompress(new File(fileName), new File(folderName), false);
}
public static void decompress(final String fileName, final String folderName, final boolean deleteZipAfter) throws IOException {
decompress(new File(fileName), new File(folderName), deleteZipAfter);
}
// Remplace l'extension si le fichier cible ne fini pas par '.zip'
private static File getZipTypeFile(final File source, final File target) throws IOException {
if (target.getName().toLowerCase().endsWith(ZIP_EXTENSION)) {
return target;
}
final String tName = target.isDirectory() ? source.getName() : target.getName();
final int index = tName.lastIndexOf('.');
return new File(new StringBuffer(target.isDirectory() ? target.getCanonicalPath() : target.getParentFile().getCanonicalPath()).append(
File.separatorChar).append(index < 0 ? tName : tName.substring(0, index)).append(ZIP_EXTENSION).toString());
}
public static void main(String[] args) throws IOException {
// ZIP.compress("C:\\Amiante_zip\\decompress", "C:\\Amiante_zip\\compress", Deflater.BEST_SPEED);
System.out.println("début de decompression " + new Date().toString());
// le rép contenant les .zip
String zipFolder = "D:\\monZipFoler";
File zipDir = new File(zipFolder);
File listeFiles[] = zipDir.listFiles();
byte[] tableauOctet = null;
try {
for (int i = 0; i < listeFiles.length; i++) {
tableauOctet = ZIP.calculerTableauByt(zipFile, listeFiles[i].getName());
System.out.println("tableauOctet du fichier " + listeFiles[i].getName() + " contient " + tableauOctet.length);
}
} catch (Exception e) {
// TODO Bloc catch auto-généré
e.printStackTrace();
}
System.out.println("fin de decomression " + new Date().toString());
}
} |
Partager