`
gao_20022002
  • 浏览: 160224 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Zip以及UnZip处理

    博客分类:
  • Java
F# 
阅读更多

早上看到一篇关于压缩文件的处理文章,测试之后发现解压缩unzip没有问题,但是对于压缩文件,不支持嵌套,对于空文件夹压缩也会出现错误。

 

修改了一下,作为加强版,记录如下:

 

第一步:工具类,组织需要压缩的文件夹下的目录以及文件。

@SuppressWarnings("unchecked")
	final static public Vector parse(File[] files, Vector v) {
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				v.add(files[i]);
			} else {
				if (null != files[i].list() && files[i].list().length == 0) {
					v.add(files[i]);
				} else {
					parse(files[i].listFiles(), v);
				}
			}
		}
		return v;
	}

第二步:处理解析出来的文件路径,变化为相对路径。

final static public String path(String base, String total) {
		return total.substring(base.length() - 3, total.length());
	}

第三步:压缩处理,注意压缩文件与压缩文件夹的区别。

	@SuppressWarnings("unchecked")
	final static public void zip(String srcfile, String destfile) {
		try {
			BufferedInputStream origin = null;
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
					new FileOutputStream(destfile)));
			byte data[] = new byte[BUFFER];
			File f = new File(srcfile);
			Vector v = new Vector();
			v = parse(f.listFiles(), v);
			File file = null;
			String path = null;
			for (int i = 0; i < v.size(); i++) {
				file = (File) v.elementAt(i);
				path = path(srcfile, file.getAbsolutePath());
				if (file.isFile()) {
					origin = new BufferedInputStream(new FileInputStream(file),
							BUFFER);
					ZipEntry entry = new ZipEntry(path);
					out.putNextEntry(entry);
					int count;
					while ((count = origin.read(data, 0, BUFFER)) != -1) {
						out.write(data, 0, count);
					}
				} else {
					ZipEntry entry = new ZipEntry(path + "/");
					out.putNextEntry(entry);
				}
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 OK,测试代码

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		zip("D://zipunzip//testzip//", "D://zipunzip//dest.zip");
		unzip("D://zipunzip//dest.zip", "D://zipunzip//testunzip//");
	}

 

 

另外:附加原始的解压缩代码:

@SuppressWarnings("unchecked")
	final static public void unzip(String srcfile, String destfile) {
		try {
			ZipFile zipFile = new ZipFile(srcfile);
			Enumeration emu = zipFile.entries();
			while (emu.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) emu.nextElement();
				if (entry.isDirectory()) {
					new File(destfile + entry.getName()).mkdirs();
					continue;
				}
				BufferedInputStream bis = new BufferedInputStream(zipFile
						.getInputStream(entry));
				File file = new File(destfile + entry.getName());
				File parent = file.getParentFile();
				if (parent != null && (!parent.exists())) {
					parent.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
				int count;
				byte data[] = new byte[BUFFER];
				while ((count = bis.read(data, 0, BUFFER)) != -1) {
					bos.write(data, 0, count);
				}
				bos.flush();
				fos.close();
				bos.close();
				bis.close();
			}
			zipFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

现在想研究一下直接使用字节流来解析压缩文件,看看相关压缩文件的格式,应该不成问题。

接下来写好了发布。

 

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics