Vous devez importer le package ant.jar et le télécharger sur le site Web Apache (http://ant.apache.org/bindownload.cgi).
La copie de code est la suivante:
Importer java.io.BufferedInputStream;
Importer java.io.BufferedOutputStream;
Importer java.io.file;
import java.io.fileInputStream;
Importer java.io.fileOutputStream;
Importer java.io.ioException;
Importer java.util.zip.zipOutputStream;
import org.apache.tools.ant.project;
import org.apache.tools.ant.taskdefs.expand;
import org.apache.tools.zip.zipentry;
import com.xyq.io.util.closeioutil;
classe publique Ziputil {
chaîne finale statique privée Encode = "UTF-8";
public static void zip (String inputFilePath, String zipFileName) {
Fichier InputFile = nouveau fichier (InputFilePath);
if (! inputfile.exists ())
lancer un nouveau RuntimeException ("Le fichier d'origine n'existe pas !!!");
Fichier BasetarzIpFile = nouveau fichier (zipFileName) .GetParentFile ();
if (! BasetarzipFile.exists () &&! BasetarzipFile.mkDirs ())
lancer un nouveau RuntimeException ("Le fichier cible ne peut pas être créé !!!");
BuffredOutputStream bos = null;
FileoutputStream out = null;
ZipOutputStream zout = null;
essayer {
// Créer un objet de sortie de fichier, invite: faites attention au support chinois
out = new FileOutputStream (new String (zipFileName.getBytes (Encode)));
BOS = nouveau tampondoutputStream (out);
// Le streaming de sortie du fichier est connecté
zout = new ZipOutputStream (BOS);
zip (zout, inputfile, inputfile.getName ());
Closeioutil.closeall (zout, bos, out);
} catch (exception e) {
e.printStackTrace ();
}
}
Private static void zip (zipOutputStream zout, fichier de fichier, base de chaîne) {
essayer {
// Si la poignée du fichier est un répertoire
if (file.isdirectory ()) {
// Obtenez le fichier dans le répertoire
File [] listFiles = file.listFiles ();
// Créer une entrée zip
zout.putNexTENTRY (new Zipentry (base + "/"));
base = (base.length () == 0? "": base + "/");
if (listFiles! = null && listfiles.length> 0)
// parcourt les fichiers dans le répertoire
pour (fichier f: listfiles)
// Entrez récursivement cette méthode
zip (zout, f, base + f.getName ());
}
// Si la poignée du fichier est un fichier
autre {
if (base == "") {
base = file.getName ();
}
// Remplissez la poignée du fichier
zout.putNextStry (new Zipentry (base));
// Démarrer la compression
// Lire à partir de Fichier Flow, écrivez à Zip Outflow
writeFile (zout, fichier);
}
} catch (exception e) {
e.printStackTrace ();
}
}
Private Static Void WriteFile (ZipOutputStream Zout, fichier de fichier)
lance iOException {
FileInputStream dans = null;
BufferedInputStream bis = null;
dans = new FileInputStream (fichier);
bis = new BufferedInputStream (in);
int len = 0;
octet [] buff = nouveau octet [2048];
while ((len = bis.read (buff))! = -1)
zout.write (buff, 0, len);
zout.flush ();
Closeioutil.closeall (bis, in);
}
/ ***
* Décompression
*
* @param zippath
* Chemin de fichier zip
* @param destinationpath
* Destination de décompression
* @param ecode
* Ensemble de caractères codé de noms de fichiers
* /
public static void unzip (String Zippath, String DestinationPath) {
Fichier zipfile = nouveau fichier (zippath);
if (! zipfile.exists ())
Jetez un nouveau RuntimeException ("Fichier Zip" + Zippath
+ "n'existe pas.");
Project Proj = nouveau projet ();
EXPAND EXPAND = NOUVEAU EXPAND ();
expand.setProject (proj);
expand.setTaskType ("unzip");
expand.setTaskName ("unzip");
expand.setsrc (zipfile);
Expand.SetDest (nouveau fichier (destinationPath));
Extend.SetEncoding (Encode);
expand.execute ();
System.out.println ("Unzip Done !!!");
}
public static void main (String [] args) {
String dir = new String ("f: // mon document de sauvegarde // MyEclipse + 9.0 Version officielle Cracking and Activation (test professionnel disponible)");
dir = new String ("f: /111.jpg");
zip (dir, "f: /bzbxb/zipant.zip");
unzip ("f: /bzbxb/zipant.zip", "f: / xx / xx /");
}
}