0

I'm trying to make a program which unzips then parses the xml file in xml.zip. Adding the dependency for zip4j, this is my pom.xml

        <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>interview</groupId>
    <artifactId>programES</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xmlToES</name>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.12.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.6.4</version>
        </dependency>

    </dependencies>
</project>

My code for the parser :

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import model.Products;
import net.lingala.zip4j.core.ZipFile;
import org.apache.commons.lang3.StringUtils;
import java.util.zip.ZipFile;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Parser {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new XmlMapper();
        String source = "C:\\Users\\user\\java_program\\xml.zip";
        String destination = "C:\\Users\\user\\java_program\\xml";
        ZipFile zipFile = new ZipFile(source);
        zipFile.extractAll(destination);
        //Reads from XML and converts to POJO
        Products products = objectMapper.readValue(
                StringUtils.toEncodedString(Files.readAllBytes(Paths.get("C:\\Users\\user\\java_program\\xml\\xml.xml")), StandardCharsets.UTF_8), Products.class);
        System.out.println(products);
    }

}

The program still write an error : The method extractAll(String) is undefined for the type ZipFile

any help is really appreciated

4

2 回答 2

0

You should import net.lingala.zip4j.core.ZipFile instead of java.util.zip.Zipfile

delete the line:

import java.util.zip.ZipFile;

and delete .core from your import import net.lingala.zip4j.core.ZipFile;

Whole class:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import model.Products;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.lang3.StringUtils;


public class Parser {
    public static void main(String[] args) throws IOException, ZipException {
        ObjectMapper objectMapper = new XmlMapper();
        String source = "C:\\Users\\user\\java_program\\xml.zip";
        String destination = "C:\\Users\\user\\java_program\\xml";
        ZipFile zipFile = new ZipFile(source);
        zipFile.extractAll(destination);
        //Reads from XML and converts to POJO
        Products products = objectMapper.readValue(
            StringUtils.toEncodedString(Files.readAllBytes(Paths.get("C:\\Users\\user\\java_program\\xml\\xml.xml")), StandardCharsets.UTF_8), Products.class);
        System.out.println(products);
    }
}
于 2021-02-07T10:32:47.133 回答
0

In your imports you are importing java.util.Zipfile so remove -

import java.util.zip.ZipFile;

and it should work.

于 2021-02-07T10:37:11.013 回答