文件压缩

wildayfd 2013-04-22 08:53:01
为什么这段压缩文件的代码不能把文件压缩成系统自带的zip格式
public static void zip() throws Exception{

FileInputStream file = new FileInputStream("D:\\EDC_FPT\\test.txt");
// 压缩文件 ,在这里调用压缩文件方法
// 定义压缩文件的名称
// 定义压缩输出流
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(new File("D:\\EDC_FPT\\test.zip")));
//实例化压缩输出流,并制定压缩文件的输出路径
zipOut.putNextEntry(new ZipEntry("D:\\EDC_FPT\\test.txt"));

int temp = 1;
while((temp = file.read()) != -1) {
zipOut.write(temp);
}
zipOut.close();
}
...全文
73 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wildayfd 2013-04-22
  • 打赏
  • 举报
回复
亲,贴个完整的方法啊~~~~
genganpeng 2013-04-22
  • 打赏
  • 举报
回复
给你个正确的代码吧~我正常使用的方法
File file = null;
		File parent = null;
		ZipFile zipFile = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			zipFile = new ZipFile(fileName);
			Enumeration<?> emu = zipFile.entries();
			while (emu.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) emu.nextElement();
				if (entry.isDirectory()) {
					new File(targetPath + "/" + entry.getName()).mkdirs();
					continue;
				}
				bis = new BufferedInputStream(zipFile.getInputStream(entry));
				file = new File(targetPath + "/" + entry.getName());
				parent = file.getParentFile();
				if (parent != null && (!parent.exists()))
					parent.mkdirs();
				fos = new FileOutputStream(file);
				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();
			}
			new File(fileName).delete();
		} catch (Exception e) {
			logger.error("===method : unzip ERROR ===" + e.getMessage());
		} finally {
			file = null;
			parent = null;
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					logger.error("===method : unzip BufferedOutputStream close ERROR ===" + e.getMessage());
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					logger.error("===method : unzip FileOutputStream close ERROR ===" + e.getMessage());
				}
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					logger.error("===method : unzip BufferedInputStream close ERROR ===" + e.getMessage());
				}
			}
			if (zipFile != null) {
				try {
					zipFile.close();
				} catch (IOException e) {
					logger.error("===method : unzip ZipFile close ERROR ===" + e.getMessage());
				}
			}
		}
  • 打赏
  • 举报
回复
报什么错?应该没有问题。
Re: 《文件备份与压缩命令》 ---------------------------------------内容提要: 1/6)tar   命令:打包备份/解压打包(将文件或目录的压缩或不解压查看查看)2/6)gzip  命令:压缩或解压文件3/6)zip   命令:打包和压缩文件4/6)unzip 命令:解压zip文件5/6)scp   命令:远程文件复制(全量备份)6/6)rsync 命令:文件同步工具(增量备份)  本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到最好,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述! 《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧