zoukankan      html  css  js  c++  java
  • Java读取压缩文件信息

    不解压压缩文件,获取其中包含的文件,通过文件名检查是否包含非法文件。(后续再根据文件头或内容吧)

    zip:

    import java.util.zip.ZipEntry;
    import java.util.zip.ZipException;
    import java.util.zip.ZipFile;

    private
    static void readZip(String fileName) { ZipFile zf = null; try { zf = new ZipFile(fileName); HashSet<ZipEntry> set = new HashSet<ZipEntry>(); int fileCount = 0; int dirCount = 0; for (Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zf.entries(); entries.hasMoreElements();) { ZipEntry ze = entries.nextElement(); if (ze.isDirectory()) { dirCount++; } else { fileCount++; String name = ze.getName(); for (String s : illegalString) { if (name.toUpperCase().contains(s)) { set.add(ze); break; } } } } System.out.println("The package contains " + fileCount + " files and " + dirCount + " directories. "); if (set.size() <= 0) { System.out.println("PASS: ILLEGAL FILE NOT FOUND."); } else { System.out.println("Possible illegal files: "); int index = 1; System.out.format(" %-10s%-80s%-20s %n%n", "NO.", "FileName", "Bytes"); for (ZipEntry e : set) { System.out.format(" %-10s%-80s%-20s %n%n", index, e.getName(), e.getSize()); index++; } } } catch (ZipException e) { System.out.println("INFO: The file format is not a common zip."); } catch (IOException e) { System.out.println("WARNING: IOException occured."); } catch (SecurityException e) { System.out.println("WARNING: The file is not accessible."); } catch (NullPointerException e) { System.out.println("WARNING: NullPointerException. May due to ZipEntry has no Size."); } finally { if (zf != null) { try { zf.close(); } catch (IOException e) { System.out.println("WARNING: IOException occured when close file."); } } } }

    tar:

    import org.apache.tools.tar.TarEntry;
    import org.apache.tools.tar.TarInputStream;

    private
    static HashSet<String> readTar(String fileName) { TarInputStream tis = null; HashSet<String> set = null; try { tis = new TarInputStream(new FileInputStream(fileName)); int dirCount = 0, fileCount = 0; set = new HashSet<String>(); TarEntry te = tis.getNextEntry(); while (te != null) { if (!te.isDirectory()) { String name = te.getName(); for (String s : illegalString) { if (name.toUpperCase().contains(s)) { set.add(name); break; } } fileCount++; } else { dirCount++; } te = tis.getNextEntry(); } System.out.println("INFO: The package contains " + dirCount + " and " + fileCount + " files."); return set; } catch (FileNotFoundException e) { System.out.println("INFO: File not exist."); } catch (IOException e) { System.out.println("WARNING: IOException occured in TarInputStream."); } finally { if (tis != null) { try { tis.close(); } catch (IOException e) { System.out.println("WARNING: IOException occured when close file."); } } } return set; }
  • 相关阅读:
    candy——动态规划
    HTTP协议(转)
    linked-list-cycle-ii——链表,找出开始循环节点
    linked-list-cycle——链表、判断是否循环链表、快慢指针
    转: utf16编码格式(unicode与utf16联系)
    【转】Nginx模块开发入门
    【转】依赖注入那些事儿
    转: OpenResty最佳实践
    转:linux的源码查看, c++语法 查看网站
    【转】所需即所获:像 IDE 一样使用 vim
  • 原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10186004.html
Copyright © 2011-2022 走看看