使用 zip4j 的 ZipFile 类型未定义方法 extractAll(String)
The method extractAll(String) is undefined for the type ZipFile with zip4j
我正在尝试制作一个解压缩然后解析 xml.zip 中的 xml 文件的程序。
添加 zip4j 的依赖项,这是我的 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>
我的解析器代码:
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);
}
}
程序仍然写入错误:方法 extractAll(String) 未定义类型 ZipFile
非常感谢任何帮助
您应该导入 net.lingala.zip4j.core.ZipFile
而不是 java.util.zip.Zipfile
删除行:
import java.util.zip.ZipFile;
并删除
.core
来自您的导入 import net.lingala.zip4j.core.ZipFile;
整个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);
}
}
在您的导入中,您正在导入 java.util.Zipfile 因此删除 -
import java.util.zip.ZipFile;
它应该可以工作。
我正在尝试制作一个解压缩然后解析 xml.zip 中的 xml 文件的程序。 添加 zip4j 的依赖项,这是我的 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>
我的解析器代码:
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);
}
}
程序仍然写入错误:方法 extractAll(String) 未定义类型 ZipFile
非常感谢任何帮助
您应该导入 net.lingala.zip4j.core.ZipFile
而不是 java.util.zip.Zipfile
删除行:
import java.util.zip.ZipFile;
并删除
.core
来自您的导入 import net.lingala.zip4j.core.ZipFile;
整个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);
}
}
在您的导入中,您正在导入 java.util.Zipfile 因此删除 -
import java.util.zip.ZipFile;
它应该可以工作。