我只能读取一个zip文件,但文件夹中并不全部都存在,下面是代码:我将使用此技术读取大多数文件,请帮助我
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.imageio.ImageIO;
public class ZipReader
{
public static void main(String[] args) throws IOException
{
ZipFile ZipFile = new ZipFile("PATH");
Enumeration<? extends ZipEntry> entries = ZipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
InputStream stream = ZipFile.getInputStream(entry);
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
Scanner inputStream = new Scanner(reader);
inputStream.nextLine();
while (inputStream.hasNext()) {
String data = inputStream.nextLine(); // Gets a whole line
System.out.println(data);
}
inputStream.close();
stream.close();
}
ZipFile.close();
}
}
代码运行后,我得到的是一个文件的输出,不是我要插入文件,而是在控制台中打印所有文件详细信息。
答案 0 :(得分:-1)
感谢引用链接,我找到了解决方法:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnZipFiles {
private static final Logger _LOGGER = LoggerFactory.getLogger(UnZipFiles.class);
public void unzipAll(String path) {
String filName;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
filName = listOfFiles[i].getName();
if (filName.endsWith(".zip") || filName.endsWith(".ZIP")) {
unZipFile(listOfFiles[i]);
}
}
}
}
public static void main(String[] args) {
UnZipFiles U = new UnZipFiles();
U.unzipAll("PATH");
}
public void unZipFile(File zipFile) {
byte[] buffer = new byte[1024];
try {
// create output directory is not exists
File folder = new File("PATH");
if (!folder.exists()) {
folder.mkdir();
}
// get the zip file content
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
// get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File("PATH" + File.separator + fileName);
if (_LOGGER.isDebugEnabled())
_LOGGER.debug("Unzipping file : {}", new Object[] {newFile.getAbsoluteFile()});
// create all non exists folders
// else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch(IOException ex) {
_LOGGER.error("Exception occurred while unzipping!", ex);
}
}
}