相关文件:

1、测试程序上传
将目录 阿帕奇解压测试Zip\程序 下的text_zip 文件夹上传服务器/root目录下。
2、测试程序运行
进入程序所在目录
1
| cd /root/test_zip/src/zip_7/
|
通过javac将.java编译为.class文件;
其编译命令如下:
1
| javac -classpath "/root/test_zip/lib/*" TestZip.java
|
通过Java执行.class文件
1
| java -cp $classpath:"/root/test_zip/lib/*":./ TestZip
|
正确现象
终端:无输出

在文件目录中
目录:/root/test_zip/src/zip_7/ 下将会多出一个名为my-test-archive的文件夹,其中有6个文件,如下图所示:

3、总结
请详细记录相关信息,测试结束后做出反馈。
程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class TestZip { public static void main(String[] args) {
ZipFile zipFile; try { zipFile = new ZipFile(new File("/root/test_zip/src/zip_7/my-test-archive.zip")); byte[] buffer = new byte[4096]; ZipArchiveEntry entry; Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); InputStream inputStream; while (entries.hasMoreElements()) { entry = entries.nextElement(); if (entry.isDirectory()) { continue; } File outputFile = new File("/root/test_zip/src/zip_7/" + entry.getName());
if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } try { inputStream = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(outputFile); while (inputStream.read(buffer) > 0) { fos.write(buffer); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e1) { e1.printStackTrace(); } } }
|