博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.util.zip - Recreating directory structure(转)
阅读量:6716 次
发布时间:2019-06-25

本文共 4832 字,大约阅读时间需要 16 分钟。

 

include my own version for your reference.

We use this one to zip up photos to download so it works with various unzip programs. It preserves the directory structure and timestamps.

public static void createZipFile(File srcDir, OutputStream out,   boolean verbose) throws IOException {  List
fileList = listDirectory(srcDir); ZipOutputStream zout = new ZipOutputStream(out); zout.setLevel(9); zout.setComment("Zipper v1.2"); for (String fileName : fileList) { File file = new File(srcDir.getParent(), fileName); if (verbose) System.out.println(" adding: " + fileName); // Zip always use / as separator String zipName = fileName; if (File.separatorChar != '/') zipName = fileName.replace(File.separatorChar, '/'); ZipEntry ze; if (file.isFile()) { ze = new ZipEntry(zipName); ze.setTime(file.lastModified()); zout.putNextEntry(ze); FileInputStream fin = new FileInputStream(file); byte[] buffer = new byte[4096]; for (int n; (n = fin.read(buffer)) > 0;) zout.write(buffer, 0, n); fin.close(); } else { ze = new ZipEntry(zipName + '/'); ze.setTime(file.lastModified()); zout.putNextEntry(ze); } } zout.close(); } public static List
listDirectory(File directory) throws IOException { Stack
stack = new Stack
(); List
list = new ArrayList
(); // If it's a file, just return itself if (directory.isFile()) { if (directory.canRead()) list.add(directory.getName()); return list; } // Traverse the directory in width-first manner, no-recursively String root = directory.getParent(); stack.push(directory.getName()); while (!stack.empty()) { String current = (String) stack.pop(); File curDir = new File(root, current); String[] fileList = curDir.list(); if (fileList != null) { for (String entry : fileList) { File f = new File(curDir, entry); if (f.isFile()) { if (f.canRead()) { list.add(current + File.separator + entry); } else { System.err.println("File " + f.getPath() + " is unreadable"); throw new IOException("Can't read file: " + f.getPath()); } } else if (f.isDirectory()) { list.add(current + File.separator + entry); stack.push(current + File.separator + f.getName()); } else { throw new IOException("Unknown entry: " + f.getPath()); } } } } return list; }}

SEPARATOR constant is initialised with the System.getProperty("file.separator") which will give me the OS default file separator.

I would never hardcode a separator since that assumes that your code will only be deployed on a given OS

Don't use File.separator in ZIP. The separator must be "/" according to the spec. If you are on Windows, you must open file as "D:\dir\subdir\file" but ZIP entry must be "dir/subdir/file"

Just go through the source of java.util.zip.ZipEntry. It treats a ZipEntry as directory if its name ends with "/" characters. Just suffix the directory name with "/". Also you need to remove the drive prefix to make it relative.

 

Here is another example (recursive) which also lets you include/exclude the containing folder form the zip:

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipUtil {  private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;  public static void main(String[] args) throws Exception {    zipFile("C:/tmp/demo", "C:/tmp/demo.zip", true);  }  public static void zipFile(String fileToZip, String zipFile, boolean excludeContainingFolder)    throws IOException {            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));        File srcFile = new File(fileToZip);    if(excludeContainingFolder && srcFile.isDirectory()) {      for(String fileName : srcFile.list()) {        addToZip("", fileToZip + "/" + fileName, zipOut);      }    } else {      addToZip("", fileToZip, zipOut);    }    zipOut.flush();    zipOut.close();    System.out.println("Successfully created " + zipFile);  }  private static void addToZip(String path, String srcFile, ZipOutputStream zipOut)    throws IOException {            File file = new File(srcFile);    String filePath = "".equals(path) ? file.getName() : path + "/" + file.getName();    if (file.isDirectory()) {      for (String fileName : file.list()) {                     addToZip(filePath, srcFile + "/" + fileName, zipOut);      }    } else {      zipOut.putNextEntry(new ZipEntry(filePath));      FileInputStream in = new FileInputStream(srcFile);      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];      int len;      while ((len = in.read(buffer)) != -1) {        zipOut.write(buffer, 0, len);      }      in.close();    }  }}

 

http://stackoverflow.com/questions/1399126/java-util-zip-recreating-directory-structure

 

转载地址:http://askmo.baihongyu.com/

你可能感兴趣的文章
Linux下配置Hadoop 1.2.1
查看>>
Fluentd 例子
查看>>
解决上传服务器端文字乱码
查看>>
java多线程(同步与死锁问题,生产者与消费者问题)
查看>>
Atitit. atiOrder Order 订单管理框架的设计
查看>>
5-2-三元组顺序表(稀疏矩阵)-数组和广义表-第5章-《数据结构》课本源码-严蔚敏吴伟民版...
查看>>
【系统】CentOS、Ubuntu、Debian三个linux比较异同
查看>>
浅谈web网站架构演变过程
查看>>
php socket 模型及效率问题
查看>>
路漫漫其修远兮,吾将上下而求索——小酌重构系列[0]开篇有益
查看>>
[javaSE] 位运算符(&|^)
查看>>
自定义控件 横向滑动控件 总结
查看>>
keil l251 command summary --Lib
查看>>
Hibernate Cascade
查看>>
java的技术调用栈图示例
查看>>
IIS 之 功能详解
查看>>
C#Base64编码
查看>>
spring主要的作用?
查看>>
Storm概念学习系列之storm的特性
查看>>
JQuery------$.get()和$.post()传递数据的使用方法
查看>>