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; }
  • 相关阅读:
    一个小企业招人的胡思乱想
    7 搜索关键词 && 提问
    6. 搜索&&学习常用站点
    5. Github的使用
    2020 renew 博客目录
    4. 版本控制
    3. 项目管理规范 && 命名方式规范
    2.7 C#语法的学习(小结)
    2.6 C#语法的学习(六) && 异常处理 && 调试
    2.5 C#语法的学习(五) && 判断
  • 原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10186004.html
Copyright © 2011-2022 走看看