ファイル圧縮java Apacheのant圧縮と解凍ファイル(zip)を使用する。
4744 ワード
章の変更はファイル圧縮に関するスレッドです。
アンセットの方法で中国語を含めたzipの圧縮と解凍を完備できます。
本体実現コードは以下の通りです。
愛、時には山の誓いの承諾がいらないで、しかし彼女はきっと入念な配慮と挨拶を必要とします。愛、時には梁祝の蝶の悲壮さが必要ではないが、彼女はきっと霊感のある暗黙の了解と投合が必要である。愛は、時には雄飛雌の追従が必要ではないが、彼女は必ず支え合い、理解する必要がある。
以上のコードはzipファイルの圧縮と解凍を実現できます。このプロセスは必要なjarパッケージをクリックして下に載せます。 リンクをダウンロード
文章は終わって、皆さんにプログラマーの笑い話の語録を共有します。プログラマがプログラムを書いたり、プログラムでお酒のお金を換えたりします。酒は目を覚ましてネット上で座って、酔ってまたネットの下で寝ます。酒に酔って目が覚めた日は再び日に戻って、ネットを利用して年を重ねます。老衰したコンピュータの間を望んで、お辞儀をしたくない支配人の前;メルセデスBMWの貴者趣味は、バスのプログラマーです。他の人が私を笑うのはあまりにも気違いで、私は自分の命が卑しいと笑う。町中のきれいな妹を見ないと、プログラマーは誰ですか?
アンセットの方法で中国語を含めたzipの圧縮と解凍を完備できます。
本体実現コードは以下の通りです。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
/**
*
* @author yaohucaizi
*/
public class ZipUtil {
/**
*
* @param file
* @param zipFile
*/
public static void zip(File file, File zipFile) {
ZipOutputStream outputStream = null;
try {
outputStream = new ZipOutputStream(new FileOutputStream(zipFile));
zipFile(outputStream, file, "");
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException ex) {
Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
outputStream.close();
} catch (IOException ex) {
Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
*
* @param output ZipOutputStream
* @param file
* @param basePath
*/
private static void zipFile(ZipOutputStream output, File file, String basePath) {
FileInputStream input = null;
try {
//
if (file.isDirectory()) {
//
File list[] = file.listFiles();
basePath = basePath + (basePath.length() == 0 ? "" : "/")
+ file.getName();
//
for (File f : list) {
zipFile(output, f, basePath);
}
} else {
//
basePath = (basePath.length() == 0 ? "" : basePath + "/")
+ file.getName();
// System.out.println(basePath);
output.putNextEntry(new ZipEntry(basePath));
input = new FileInputStream(file);
int readLen = 0;
byte[] buffer = new byte[1024 * 8];
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
output.write(buffer, 0, readLen);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
//
if (input != null) {
try {
input.close();
} catch (IOException ex) {
Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/**
*
* @param sourceZip
* @param destDir
*/
public static void unZip(String sourceZip, String destDir) {
// "/" "\"
char lastChar = destDir.charAt(destDir.length() - 1);
if (lastChar != '/' && lastChar != '\\') {
destDir += File.separator;
}
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(sourceZip));
e.setOverwrite(false);
e.setDest(new File(destDir));
/*
ant zip UTF-8 ,
winRAR windows GBK GB2312
*/
e.setEncoding("gbk");
e.execute();
}
public static void main(String[] args) {
String sourcePath = "C:/model.zip";
String destPath = "C:/test";
unZip(sourcePath, destPath);
zip(new File("C:/test/model"), new File("d:/model.zip"));
}
}
毎日の道理愛、時には山の誓いの承諾がいらないで、しかし彼女はきっと入念な配慮と挨拶を必要とします。愛、時には梁祝の蝶の悲壮さが必要ではないが、彼女はきっと霊感のある暗黙の了解と投合が必要である。愛は、時には雄飛雌の追従が必要ではないが、彼女は必ず支え合い、理解する必要がある。
以上のコードはzipファイルの圧縮と解凍を実現できます。このプロセスは必要なjarパッケージをクリックして下に載せます。 リンクをダウンロード
文章は終わって、皆さんにプログラマーの笑い話の語録を共有します。プログラマがプログラムを書いたり、プログラムでお酒のお金を換えたりします。酒は目を覚ましてネット上で座って、酔ってまたネットの下で寝ます。酒に酔って目が覚めた日は再び日に戻って、ネットを利用して年を重ねます。老衰したコンピュータの間を望んで、お辞儀をしたくない支配人の前;メルセデスBMWの貴者趣味は、バスのプログラマーです。他の人が私を笑うのはあまりにも気違いで、私は自分の命が卑しいと笑う。町中のきれいな妹を見ないと、プログラマーは誰ですか?