怎样压缩文件,zip格式的,求代码!在线等... 从来没接触过压缩,求个代码,急用! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java.util.zip 去这个包下找 吧 // 要压缩的文件,包含多个文件 String[] filenames = new String[]{"filename1", "filename2"}; // 创建一个字节数组用于读取文件 byte[] buf = new byte[1024]; try { // 指定压缩后的文件名字 String outFilename = "outfile.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); // 压缩文件 for (int i=0; i<filenames.length; i++) { FileInputStream in = new FileInputStream(filenames[i]); out.putNextEntry(new ZipEntry(filenames[i])); // 压缩 int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } catch (IOException e) { } try { FileOutputStream fos = new FileOutputStream("c:/test/a.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(fos); // --- zipOutputStream.putNextEntry(new ZipEntry("file_a.txt")); // 根路径中的文件 zipOutputStream.write(new byte[] { 65, 65, 65 }); // 文件内容 zipOutputStream.closeEntry(); // --- zipOutputStream.putNextEntry(new ZipEntry("dir/file_b.txt")); // 目录下的文件 zipOutputStream.write(new byte[] { 66, 66, 66 }); // 文件内容 zipOutputStream.closeEntry(); // --- zipOutputStream.putNextEntry(new ZipEntry("empty_dir/")); // 空目录 zipOutputStream.closeEntry(); // --- zipOutputStream.close(); // 一定要调用close()方法,否则生成的zip文件是不完整的 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 大师们!我又来了。。 使用iText的UniGB-UCS2-H编码方式进行中文编码出现的标点符号的问题 俺是新手,请帮帮小弟,在java中如何自定义一个日期域,格式为:[ - - ]. The JAVA Web Server Tutorial书名的中文译名是什么呀? 请问:JAVA中怎样调用外部程序? 一个很入门的问题 一个GUI的烤鸭程序, 可难坏我这个菜鸟了, 各位大虾帮一把吧, 多谢了. package问题 谁能介绍一下 Arraylist Linklist 中 addAll() 是如何实现的? 又没结果?一切工夫白费了。 ObjectStream能在网上转输吗? 线程之谜
去这个包下找 吧
// 要压缩的文件,包含多个文件
String[] filenames = new String[]{"filename1", "filename2"};
// 创建一个字节数组用于读取文件
byte[] buf = new byte[1024];
try {
// 指定压缩后的文件名字
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// 压缩文件
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
out.putNextEntry(new ZipEntry(filenames[i]));
// 压缩
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
}
FileOutputStream fos = new FileOutputStream("c:/test/a.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
// ---
zipOutputStream.putNextEntry(new ZipEntry("file_a.txt")); // 根路径中的文件
zipOutputStream.write(new byte[] { 65, 65, 65 }); // 文件内容
zipOutputStream.closeEntry();
// ---
zipOutputStream.putNextEntry(new ZipEntry("dir/file_b.txt")); // 目录下的文件
zipOutputStream.write(new byte[] { 66, 66, 66 }); // 文件内容
zipOutputStream.closeEntry();
// ---
zipOutputStream.putNextEntry(new ZipEntry("empty_dir/")); // 空目录
zipOutputStream.closeEntry();
// ---
zipOutputStream.close(); // 一定要调用close()方法,否则生成的zip文件是不完整的
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}