2011年7月11日月曜日

ZIPファイルの解凍

ZIPファイルの解凍


/*
 * targetDir:解凍するファイルのパス
 * creatDir:生成するファイルの親ディレクトリ
 */
private static void Unzip( String targetDir , String creatDir) {
int bufferSize = 4096; //4kb
String entryName;
BufferedOutputStream bos = null; // バッファー出力

try {

FileInputStream fis = new FileInputStream(targetDir);
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(fis));

ZipEntry zipEntry; //
while ((zipEntry = zis.getNextEntry()) != null) {

try {

int count;
byte data[] = new byte[bufferSize];

//ファイル名
entryName = zipEntry.getName();

File entryFile = new File(creatDir+ entryName);

//親フォルダーがない場合、新規生成
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}

FileOutputStream fos = new FileOutputStream(entryFile);

bos = new BufferedOutputStream(fos, bufferSize);
while ((count = zis.read(data, 0, bufferSize)) != -1) {
bos.write(data, 0, count);

}
bos.flush();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
} catch (Exception cwj) {
cwj.printStackTrace();
}
}

0 件のコメント: