我一直在尝试使用java来提取程序中的一些zip文件并将它们放在正确的目录中。我一直在寻找这个参考: http://javabeginnerstutorial.com/code-base/how-to-extract-zip-file-with-subdirectories/
我当前的代码看起来像
String ziplocation = "/home/user/folder/";
String zipfile = "libraries.zip";
String liblocation = "/home/user/folder";
ZipEntry zipentry;
if (!zipentry.isDirectory())
{
File fileFile = new File(liblocation + "/" + zipentry.getName());
InputStream inputstream = zipfile.getInputStream(zipentry);
String str = new java.util.Scanner(inputstream).useDelimiter("/A").next();
BufferedWriter bw = new BufferedWriter(new FileWriter(fileFile));
bw.append(str);
bw.close();
}
似乎所有东西都从上面的参考文献中正确改编,但是当我编译它时,它给了我:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getInputStream(ZipEntry) is undefined for the type String
at Main.main(Main.java:53)
编辑:
我现在已将其更改为
String ziplocation = "/home/user/folder/zipfile.zip";
String liblocation = "/home/user/folder/";
File file = new File(ziplocation);
ZipFile zipfile = new ZipFile(file);
InputStream inputStream = new BufferedReader(new FileReader(zipfile));
ZipEntry zipentry;
if (!zipentry.isDirectory())
{
File fileFile = new File(liblocation + "/" + zipentry.getName());
InputStream inputstream = zipfile.getInputStream(zipentry);
String str = new java.util.Scanner(inputstream).useDelimiter("/A").next();
BufferedWriter bw = new BufferedWriter(new FileWriter(fileFile));
bw.append(str);
bw.close();
}
但是现在编译器正在响应:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from BufferedReader to InputStream
The constructor FileReader(ZipFile) is undefined
at Main.main(Main.java:48)
答案 0 :(得分:0)
这意味着字符串对象没有getInputStream方法。在这一行: zipfile.getInputStream(ZipEntry的);
zipfile是一个字符串。
你需要类似的东西 InputStream inputStream = new BufferedReader(new FileReader(zipfile));
这里对IO流进行了很好的讨论:http://docs.oracle.com/javase/tutorial/essential/io/streams.html
答案 1 :(得分:0)
问题在于:
InputStream inputStream = new BufferedReader(new FileReader(zipfile));
该句子不在您关注的示例中。相反,在 zipfile 中找到的元素有一个循环:
File file = new File(zipFileLocation);
ZipFile zipfile = new ZipFile(file);
ZipEntry zipentry;
System.out.println("nList of files in zip archive");
int fileNumber = 0;
for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e
.hasMoreElements(); fileNumber++) {
zipentry = e.nextElement();
if (!zipentry.isDirectory()) {
...