Javaでディレクトリ圧縮(子ディレクトリ非対応)を実現するサンプルコード
Javaコード:
package aestapp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipTestStartnews24 {
public static void main(String[] args) {
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
File[] files = new File(“D:\\iphonegame").listFiles();
File f=new File(“D:\\iphonegame.zip");
try {
fos = new FileOutputStream(f);
zipOut = new ZipOutputStream(fos,Charset.forName(“utf-8"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
f.delete();
return;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(files[i]);
ZipEntry entry = new ZipEntry(files[i].getName());
zipOut.putNextEntry(entry);
int nNumber;
byte[] buffer = new byte[Integer.MAX_VALUE/32];
while ((nNumber = fis.read(buffer)) != -1)
zipOut.write(buffer, 0, nNumber);
zipOut.flush();
} catch (IOException e) {
e.printStackTrace();
try {
zipOut.close();
fos.close();
f.delete();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
} finally {
try {
fis.close();
} catch (IOException e) {
}
}
}
}
try {
zipOut.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}