Javaの直接経由し、zipファイルおよびエラー処理の内容を読みます

圧縮されたパケットからデータ項目を読み、データを処理する必要があります。ただ、その解凍を考え、そして読み始めました。そして私は、直接読み取りを見つけました。主のZipEntryを使用しています。そうネスト読み出される圧縮されたパケットは、複数の考えは、圧縮袋アウトを抽出するために圧縮されます。

直接コードに:


/** 
 * 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();
	}
}

エラー処理:Javaを解凍zip java.lang.IllegalArgumentExceptionが:MALFORMED間違っています!

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]

ソリューション: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);

 

公開された295元の記事 ウォン称賛37 ビュー30000 +

おすすめ

転載: blog.csdn.net/tianshan2010/article/details/104786524