Skip to content

Instantly share code, notes, and snippets.

@kazua
Last active December 16, 2015 14:59
Show Gist options
  • Save kazua/5452277 to your computer and use it in GitHub Desktop.
Save kazua/5452277 to your computer and use it in GitHub Desktop.
圧縮用プログラム(階層対応)
//write kazua
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class zipCreate {
private void zipFileMake(String dir, String zipfile) throws Exception {
File zippath = new File(zipfile);
if (!zippath.getParentFile().exists()) zippath.getParentFile().mkdirs();
ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(zipfile));
zo.setEncoding("MS932");
File tgtdir = new File(dir);
File[] tgtpaths = tgtdir.listFiles();
for (int i = 0; i < tgtpaths.length; i++) {
zipFileMakeProc(zo, tgtpaths[i], "");
}
zo.flush();
zo.close();
}
private void zipFileMakeProc(ZipOutputStream zo, File path, String hrc)
throws Exception {
if (path.isDirectory()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
zipFileMakeProc(zo, files[i], hrc + path.getName() + "/");
}
} else {
ZipEntry entry = new ZipEntry(hrc
+ path.getName().replace("\\", "/"));
zo.putNextEntry(entry);
byte buf[] = new byte[1024];
int size;
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(path.getPath()));
while ((size = in.read(buf, 0, 1024)) != -1) {
zo.write(buf, 0, size);
}
zo.closeEntry();
in.close();
}
}
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date now = new Date();
zipCreate zc = new zipCreate();
zc.zipFileMake("D:/写真", "E:/backup/家族写真" + sdf.format(now) + ".zip");
} catch (Exception e) {
System.out.println(e.toString());
if (e.getCause() != null)
for (StackTraceElement ste : e.getCause().getStackTrace())
System.out.println(ste.toString());
else
for (StackTraceElement ste : e.getStackTrace())
System.out.println(ste.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment