作者:zyl910
现在zip类的文件越来越多了,例如jar、docx。
有时我们需批量处理这些文件中的数据,若都是手工操作的话就太麻烦了。于是考虑编程自动处理。
Java提供了ZipInputStream等zip的操作类。但是有些内容比较抽象,没有代码范例的话有点难以理解。例如zip中的目录究竟是什么。
于是我做个个Demo来演示如何用它来解析zip文件,输出信息。
import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipReadTest { public static void main(String[] args) { String srcPath = "target/classes/static/test.docx"; try(FileInputStream is = new FileInputStream(srcPath)) { run(System.out, is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("ZipReadTest done."); } private static void run(PrintStream out, InputStream is) throws IOException { try(ZipInputStream zis = new ZipInputStream(is)) { ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { if (null==zipEntry) continue; String line = String.format("ZipEntry(%s, isDirectory=%d, size=%d, compressedSize=%d, time=%d, crc=%d, method=%d, comment=%s)", zipEntry.getName(), (zipEntry.isDirectory())?1:0, zipEntry.getSize(), zipEntry.getCompressedSize(), zipEntry.getTime(), zipEntry.getCrc(), zipEntry.getMethod(), zipEntry.getComment()); out.println(line); } } } }
对一个docx文件进行处理,测试结果如下:
ZipEntry([Content_Types].xml, isDirectory=0, size=1344, compressedSize=354, time=312739200000, crc=1239027019, method=8, comment=null) ZipEntry(docProps/app.xml, isDirectory=0, size=289, compressedSize=183, time=312739200000, crc=4091958464, method=8, comment=null) ZipEntry(docProps/core.xml, isDirectory=0, size=392, compressedSize=220, time=312739200000, crc=3065131828, method=8, comment=null) ZipEntry(word/document.xml, isDirectory=0, size=36724, compressedSize=4017, time=312739200000, crc=1830572178, method=8, comment=null) ZipEntry(word/fontTable.xml, isDirectory=0, size=2120, compressedSize=398, time=312739200000, crc=268271083, method=8, comment=null) ZipEntry(word/numbering.xml, isDirectory=0, size=2868, compressedSize=502, time=312739200000, crc=3349595521, method=8, comment=null) ZipEntry(word/settings.xml, isDirectory=0, size=815, compressedSize=325, time=312739200000, crc=773262145, method=8, comment=null) ZipEntry(word/styles.xml, isDirectory=0, size=8628, compressedSize=1319, time=312739200000, crc=2957985579, method=8, comment=null) ZipEntry(_rels/.rels, isDirectory=0, size=588, compressedSize=228, time=312739200000, crc=1578830051, method=8, comment=null) ZipEntry(word/media/image1.emf, isDirectory=0, size=881176, compressedSize=97920, time=312739200000, crc=1974652776, method=8, comment=null) ZipEntry(word/_rels/document.xml.rels, isDirectory=0, size=980, compressedSize=283, time=312739200000, crc=2086150245, method=8, comment=null) ZipReadTest done.
以上就是docx内的文件结构。
平时使用WinZip等文件来查看zip类文件时,是显示为树形结构。但是在zip文件中实际上记录的是相对路径。
zip文件类一般不用记录目录,只记录相对路径就行。
当需要记录目录时,ZipEntry的name以分隔符结尾(如 word/
),相关数据流为空。