Java directly through and read the contents of the zip file and error handling

There is a need to read the data item from the compressed packet, and process the data. Just began to think that decompress and then read. And then I found a direct read. Mainly uses ZipEntry. A plurality of compressed packets to be read so nested, the idea is compressed to extract the compressed bag out.

Directly on the code:


/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ruoyi.parser;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * @desc: 
 * @name: FileParser.java
 * @author: tompai
 * @email:[email protected]
 * @createTime: 2020年3月10日 下午11:13:33
 * @history:
 * @version: v1.0
 */

public class FileParser {

	/**
	 * @author: tompai
	 * @createTime: 2020年3月10日 下午11:13:34
	 * @history:
	 * @param args void
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String fileName="./doc/123.zip";
		String txt;
		try {
			txt = readZip(fileName);
			System.out.println(txt);
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static String readZip(String fileName) throws IOException {
		//不加编码,处理可能会报错:MALFORMED 错误
		File file = new File(fileName);
		String parentZipParent = file.getParent();// 获取上级文件夹解压到这里
		File temp = file;
		InputStream in=new FileInputStream(fileName);
		
		BufferedInputStream bis = new BufferedInputStream(in);
		String code = CodeDetector.getEncode(bis, false);
		Charset cs=Charset.forName(code);
		ZipFile zip = new ZipFile(fileName,cs);
		ZipInputStream zis = new ZipInputStream(bis,cs);
		ZipEntry entry;
		// 用于获取压缩文件中的文件或文件夹
		StringBuffer sb = new StringBuffer();
		while ((entry = zis.getNextEntry()) != null) {
			if (entry.isDirectory()) {
				System.out.println("文件夹");
			} else {
				// System.out.println("file:"+entry.getName());
				if (entry.getName().endsWith("txt")) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(zip.getInputStream(entry)));
					String line = null;
					while ((line = reader.readLine()) != null) {
						// System.out.println(line);
						sb.append(line);
					}
				} else if (entry.getName().endsWith("zip")) { // 判断是否为压缩包,若是则将其解压出再读取
					temp = new File(parentZipParent + "\\" + entry.getName());
					// System.out.println(temp.getAbsolutePath());
					if (!temp.getParentFile().exists()) {
						temp.getParentFile().mkdirs();
					}
					OutputStream os = new FileOutputStream(temp);
					//// 通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
					InputStream is = zip.getInputStream(entry);
					int len = 0;
					while ((len = is.read()) != -1) {
						os.write(len);
					}
					sb.append(readZip(temp.getAbsolutePath()));
				}
			}
		}
		return sb.toString();
	}
}

Error process: java-extracting zip java.lang.IllegalArgumentException: MALFORMED wrong!

java.lang.IllegalArgumentException: MALFORMED[1]
    at java.util.zip.ZipCoder.toString(ZipCoder.java:65) ~[na:1.8.0_181]
    at java.util.zip.ZipFile.getZipEntry(ZipFile.java:583) ~[na:1.8.0_181]
    at java.util.zip.ZipFile.access$900(ZipFile.java:60) ~[na:1.8.0_181]
    at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:539) ~[na:1.8.0_181]
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:514) ~[na:1.8.0_181]
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:495) ~[na:1.8.0_181]

Solution: CodeDetector

InputStream in=new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(in);
//检测编码格式
String code = CodeDetector.getEncode(bis, false);
Charset cs=Charset.forName(code);
ZipFile zip = new ZipFile(fileName,cs);
ZipInputStream zis = new ZipInputStream(bis,cs);

 

Published 295 original articles · won praise 37 · views 30000 +

Guess you like

Origin blog.csdn.net/tianshan2010/article/details/104786524