import java.io.*;
import java.util.zip.*;
/*
* Creates a ZIP file to be read by Winzip or Winrar
*/
public class WriteZip {
public static void main(String[] args) {
// lets put 2 files
String[] filename = {"file1.dat", "file2.dat"};
// create a line of text for the files
byte[] buffer = new byte[27]; // the alphabet + <line feed?
byte letter = 'a';
for(int i = 0; i < 26; ++i)
buffer[i] = letter++;
buffer[26] = '\n'; // and an end of line
try {
// Create the ZIP file
String outFilename = "myzip.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress two files
for (int i=0; i<filename.length; i++) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filename[i]));
// Transfer 10 lines into the file
for(int j = 0; j < 10; ++j)
out.write(buffer);
// Close the file
out.closeEntry();
}
// Close the ZIP file
out.close();
} catch (IOException e) {
System.out.println("Problem writing ZIP file: " + e);
}
}
}
import java.io.*;
import java.util.zip.*;
public class ReadZip {
public static void main(String[] args) {
try {
// Open the ZIP file
String inFilename = "myzip.zip";
ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
// While we have other entry
ZipEntry entry = in.getNextEntry();
while(entry != null){
System.out.println("Reading: " + entry.getName());
// Transfer bytes from the ZIP file to the output file
// OK I know that my file contains 10 * 27 bytes the reading might
// be more versatile and test if there is more bytes in the file
byte[] buf = new byte[1024];
int len = in.read(buf);
String theFile = new String(buf, 0, len);
System.out.println(theFile);
entry = in.getNextEntry();
} // end while
// Close the ZIP file
in.close();
} catch (IOException e) {
System.out.println("Probel reading back the ZIP file: " + e);
}
}
}
参考:http://www.dreamincode.net/forums/topic/244086-write-and-read-zip-file-from-java/
相关推荐
基于ZipLib(C语言读写zip文件的库)开发的封装类,方便压缩和解压zip文件,附演示代码
编程方式读写ZIP文件(C++).rar
VC读写操作文件.zip visual c++读写操作文件.zip
能够把 Zip文件或内存缓存到新的或现有的 ...直接读写多个部分的 zip文件到硬盘。 从另一个 zip文件的内容或合并两个 zip文件到一起来更新一个 zip文件 可选择后台处理,使你的代码可以在 Xceed Zip工作时继续执行。
读写xml文件的例子.zip读写xml文件的例子.zip
c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件读写操作代码c语言文件...
本资源工具包是自行封装使用Jmeter读取和写入Excel文件的方法,里面包含相关jar包和脚本范例,可通过此方法读取接口测试用例执行,并把执行结果写入到excel中。同时支持xls和xlsx两种文件格式,希望对大家有所帮助。
读写大容量文件时比较有用的
源码 14、stm32单片机之W25Q64存储器的读写.zip源码 14、stm32单片机之W25Q64存储器的读写.zip源码 14、stm32单片机之W25Q64存储器的读写.zip源码 14、stm32单片机之W25Q64存储器的读写.zip源码 14、stm32单片机之...
APIHook文件监控 监控指定目录下文件的读写和修改.zip
Android应用源码SdCard读写文件实例.zip
powerbuilder 读/写excel文件示例,系统不需要安装office excel,wps或其他第三方软件或程序,支持xls,xlsx,et文件格式的读写。
Python-xlsx 主要用于读写Microsoft Office 2007及更高版本.xlsx文件的库。Python-xlsx 也配套库了对ppt、doc文件读写的python-PPTX和python-DOCX。 标签:Python
VC++读写excle文件
Qt版本: 5.12.6 实现功能: 在Android设备上动态申请磁盘存储权限,在手机磁盘上创建目录,创建文件,读写文件。
js处理文件——文件读写例子.zip,很不错的例子!
c语言文件读写操作代码。C语言作为一门广泛应用的编程语言,其文件读写功能是处理数据不可或缺的一部分。在C语言中,文件读写操作主要通过标准I/O库(stdio.h)来实现。这些操作允许程序与存储设备之间进行数据的...
Python自带模块zipfile可以完成zip压缩文件的读写,而且使用非常方便,下面我们就来演示一下Python读写zip文件的实例代码,需要的朋友参考下吧
C语言配置文件读写.zip
vxd驱动开发之监控文件的读写,监视文件操作.zip