• 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; }
  • 相关阅读:
    url
    松弛时间
    Linux下为当前用户添加 PYTHONPATH 环境变量
    ElasticSearch集群的安装(windows)
    软件开发安全
    java,判断手机设备跟adb建立连接
    question
    氚云后台代码小栗子,流程表单新增完成反写源单状态
    November Challenge 2020 Division 1
    February Challenge 2021 Division 1 选做
  • 原文地址:https://www.cnblogs.com/chenhuanBlogs/p/10186004.html
Copyright © 2020-2023  润新知