代码之家  ›  专栏  ›  技术社区  ›  BullyWiiPlaza

使用zip4j解包zip文件时出现“负时间”异常

  •  0
  • BullyWiiPlaza  · 技术社区  · 7 年前

    我正在使用 zip4j 提取 zip Windows 8 用户正在获取以下异常:

    net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: java.lang.IllegalArgumentException: Negative time
        at net.lingala.zip4j.unzip.Unzip.initExtractFile(Unzip.java:163)
        at net.lingala.zip4j.unzip.Unzip.initExtractAll(Unzip.java:83)
        at net.lingala.zip4j.unzip.Unzip.extractAll(Unzip.java:73)
        at net.lingala.zip4j.core.ZipFile.extractAll(ZipFile.java:488)
        at net.lingala.zip4j.core.ZipFile.extractAll(ZipFile.java:451)
        ...
    

    负面时间似乎是由 file on the file system having a negative time and/or by a JVM bug . 有人知道如何解决这个问题吗?因为这很奇怪,而且和我使用 API

    zip4j 2013 拉链 图书馆除了图书馆外没有样板 JDK that isn't supported by the JDK .

    安装 JDK 11

    1 回复  |  直到 7 年前
        1
  •  0
  •   BullyWiiPlaza    7 年前

    在做了更多的研究之后,我发现 7-Zip-JBinding :

    <dependency>
        <groupId>net.sf.sevenzipjbinding</groupId>
        <artifactId>sevenzipjbinding</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>net.sf.sevenzipjbinding</groupId>
        <artifactId>sevenzipjbinding-all-platforms</artifactId>
        <version>LATEST</version>
    </dependency>
    

    以下代码可用于提取受密码保护的zip文件:

    public static void unzipUsing7Zip(String zipFilePath,
                                       String destinationDirectory,
                                       String password) throws IOException
    {
        try (val randomAccessFile = new RandomAccessFile(zipFilePath, "r");
             val randomAccessFileInStream = new RandomAccessFileInStream(randomAccessFile);
             val inArchive = openInArchive(null, randomAccessFileInStream))
        {
            val simpleInArchive = inArchive.getSimpleInterface();
            val archiveItems = simpleInArchive.getArchiveItems();
    
            for (val archiveItem : archiveItems)
            {
                if (!archiveItem.isFolder())
                {
                    val archiveItemPath = archiveItem.getPath();
                    val targetFilePath = destinationDirectory + separator + archiveItemPath;
    
                    try (val fileOutputStream = new FileOutputStream(targetFilePath))
                    {
                        archiveItem.extractSlow(data ->
                        {
                            try
                            {
                                if (archiveItemPath.indexOf(separator) > 0)
                                {
                                    // Create parent folder(s)
                                    val lastSeparatorIndex = archiveItemPath.lastIndexOf(separator);
                                    val path = destinationDirectory + separator + archiveItemPath.substring(0, lastSeparatorIndex);
                                    createDirectories(Paths.get(path));
                                }
    
                                fileOutputStream.write(data);
                            } catch (Exception exception)
                            {
                                exception.printStackTrace();
                            }
    
                            return data.length;
                        }, password);
                    }
                }
            }
        }
    }
    

    here 但经过清理和真正的文件提取代码通过 FileOutputStream 补充。

    val 来自 lombok 当然

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>LATEST</version>
        <scope>provided</scope>
    </dependency>
    
        2
  •  0
  •   phwt    5 年前

    我发现问题出在 . 格式不匹配。您可以通过以下方式进行检查:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  
    LocalDateTime now = LocalDateTime.now();  
    System.out.println(dtf.format(now));  
    
    File file = new File("folder/source.zip");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    System.out.println(sdf.format(file.lastModified()));
    

    区域设置 区域格式 &燃气轮机&燃气轮机; 英语(美国)

    推荐文章