zip压缩/解压缩带空文件夹的文件

本文提供了Java代码示例,详细说明如何进行带空文件夹的Zip文件压缩和解压缩操作。代码包括递归压缩文件和文件夹的函数zipFileWithTier以及解压缩函数unZipFileWithTier,适用于需要处理多层级文件结构的场景。

       在EncryptZip的项目中对Zip的加密/解密没有包含带子文件夹的需求, 只有一层的关系. 

在此文中给出普通的zip压缩/解压缩的Java代码,  压缩时递归压缩文件,包含文件及文件下的空文件夹. 若Flex端需要类似的功能, 可以参考本文中的zipFileWithTier 和unZipFileWithTier方法.

  代码如下:

        

public class ZipFileWithTier {

    private static final String zipPath = "C:\\temp\\Lemur\\empty.zip";
    private static final String unzipPath = "C:\\temp\\Lemur\\";
    private static final String srcFiles = "C:\\temp\\Lemur\\empty";

    @Test
    public void zipFile(){
	 File file = new File(zipPath);
	 if(file.exists())
	 file.delete();
	 zipFileWithTier(srcFiles, zipPath);
    }
    
    @Test
    public void upZipFile(){
	try {
	    unzipFilesWithTier(readFileByte(zipPath), unzipPath + File.separator);
	} catch (IOException e) {
	    
	    e.printStackTrace();
	}
    }
    
    /*
     * Compress the specify file that contains sub-folders and store them to a zipfile.
     * @param srcFiles: the file will be compressed
     * @param zipPath: the location which stores the zipfile.
     */
    public static void zipFileWithTier(String srcFiles, String zipPath) {
	try {

	    FileOutputStream zipFile = new FileOutputStream(zipPath);
	    BufferedOutputStream buffer = new BufferedOutputStream(zipFile);
	    ZipOutputStream out = new ZipOutputStream(buffer);
	    zipFiles(srcFiles, out, "");
	    out.close();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    /*
     * Recursive the specify file also contains the folder which may don't include any file.
     * @param filePath: compress file
     * @param ZipOutputStream: the zipfile's outputStream. 
     * @param prefix: the prefix indicates the parent folder name of the file that makes the tier relation.
     */
    public static void zipFiles(String filePath, ZipOutputStream out, String prefix)
	    throws IOException {
	File file = new File(filePath);
	if (file.isDirectory()) {
	    if (file.listFiles().length == 0) {
		ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/");
		out.putNextEntry(zipEntry);
		out.closeEntry();
	    } else {
		prefix += file.getName() + File.separator;
		for (File f : file.listFiles())
		    zipFiles(f.getAbsolutePath(), out, prefix);
	    }
	} else {
	    FileInputStream in = new FileInputStream(file);
	    ZipEntry zipEntry = new ZipEntry(prefix + file.getName());
	    out.putNextEntry(zipEntry);
	    byte[] buf = new byte[1024];
	    int len;
	    while ((len = in.read(buf)) > 0) {
		out.write(buf, 0, len);
	    }
	    out.closeEntry();
	    in.close();
	}

    }

    /*
     * Unzip the file also contains the folder which the listFiles's length is 0.
     * @param bytes: the content of the zipfile by byte array.     * 
     * @param prefix: the prefix is the root of the store path.
     * @IOExcetion: the ioexception during unzipFiles.
     */
    public static void unzipFilesWithTier(byte[] bytes, String prefix) throws IOException {

	InputStream bais = new ByteArrayInputStream(bytes);
	ZipInputStream zin = new ZipInputStream(bais);
	ZipEntry ze;
	while ((ze = zin.getNextEntry()) != null) {
	    if (ze.isDirectory()) {
		File file = new File(prefix + ze.getName());
		if (!file.exists())
		    file.mkdirs();
		continue;
	    }
	    File file = new File(prefix + ze.getName());
	    if (!file.getParentFile().exists())
		file.getParentFile().mkdirs();
	    ByteArrayOutputStream toScan = new ByteArrayOutputStream();
	    byte[] buf = new byte[1024];
	    int len;
	    while ((len = zin.read(buf)) > 0) {
		toScan.write(buf, 0, len);
	    }
	    byte[] fileOut = toScan.toByteArray();
	    toScan.close();
	    writeByteFile(fileOut, new File(prefix + ze.getName()));
	}
	zin.close();
	bais.close();
    }

    public static byte[] readFileByte(String filename) throws IOException {

	if (filename == null || filename.equals("")) {
	    throw new NullPointerException("File is not exist!");
	}
	File file = new File(filename);
	long len = file.length();
	byte[] bytes = new byte[(int) len];

	BufferedInputStream bufferedInputStream = new BufferedInputStream(
		new FileInputStream(file));
	int r = bufferedInputStream.read(bytes);
	if (r != len)
	    throw new IOException("Read file failure!");
	bufferedInputStream.close();

	return bytes;

    }
    
    public static String writeByteFile(byte[] bytes, File file) {
	FileOutputStream fos = null;
	try {
	    fos = new FileOutputStream(file);
	    fos.write(bytes);
	} catch (FileNotFoundException e) {
	    e.printStackTrace();

	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if (fos != null) {
		try {
		    fos.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
	return "success";
    }

  

}

(转贴请注明出处) 

Author:David

   Mail:xiang.okay@gmail.com


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值