解决java ZipInputStream 读取中文内容的乱码

package com.sinosoft.jbs.sevices.hxlife.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Util {
	// 日志对象
	private static Logger log = Logger.getLogger(Util.class);

	/**
	 * parse zip File xml content
	 * 
	 * @param file
	 * @return
	 */
	public static String parseZIP(String file) {
		if (null != file && !"".equals(file)) {
			String suffix = file.substring(file.lastIndexOf('.') + 1, file
					.length());
			if ("zip".equals(suffix)) {
				try {
					InputStream ins = new FileInputStream(new File(file)); // URLConnection
					// .getInputStream();
					ZipInputStream zipInput = new ZipInputStream(ins);
					@SuppressWarnings("unused")
					ZipEntry zipEntry = null;
					String xml = "";// content
					BufferedReader in = null;
					while ((zipEntry = zipInput.getNextEntry()) != null) {

						/*
						 * byte[] buf = new byte[1024]; int len=-1;
						 * 
						 * while ((len=zipInput.read(buf))!=-1) { xml+=new
						 * String(buf,0,len,oscode); }
						 */
						in = new BufferedReader(new InputStreamReader(zipInput,
								"GBK"));
						StringBuffer buffer = new StringBuffer();
						String line = "";
						while ((line = in.readLine()) != null) {
							buffer.append(line);
						}
						xml = buffer.toString();
						zipInput.closeEntry();

					}
					zipInput.close();
					ins.close();
					if (in != null) {
						in.close();
					}
					return xml;

				} catch (FileNotFoundException e) {
					log.error("parse zip file FileNotFoundException:"
							+ e.getMessage());

				} catch (IOException e) {

					log.error("parse zip file IOException:" + e.getMessage());
				}
				return null;
			}
			return null;

		}
		return null;
	}

	/**
	 * parse zip Stream xml content
	 * 
	 * @param file
	 * @return
	 */
	public static String parseZIP(InputStream ins) {
		if (null != ins) {
			try {
				ZipInputStream zipInput = new ZipInputStream(ins);
				@SuppressWarnings("unused")
				ZipEntry zipEntry = null;
				String xml = "";// content
				BufferedReader in = null;
				while ((zipEntry = zipInput.getNextEntry()) != null) {

					/*
					 * byte[] buf = new byte[1024]; int len=-1;
					 * 
					 * while ((len=zipInput.read(buf))!=-1) { xml+=new
					 * String(buf,0,len,oscode); }
					 */
					in = new BufferedReader(new InputStreamReader(zipInput,
							"GBK"));
					StringBuffer buffer = new StringBuffer();
					String line = "";
					while ((line = in.readLine()) != null) {
						buffer.append(line);
					}
					xml = buffer.toString();
					zipInput.closeEntry();

				}
				zipInput.close();
				ins.close();
				if (in != null) {
					in.close();
				}
				return xml;

			} catch (FileNotFoundException e) {
				log.error("parse zip file FileNotFoundException:"
						+ e.getMessage());

			} catch (IOException e) {

				log.error("parse zip file IOException:" + e.getMessage());
			}
			return null;

		}
		return null;
	}

	/**
	 * base64 decryption
	 * 
	 * @param x
	 *            decryption message
	 * @return
	 */
	public static String base64Decoder(String x) {
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] encode = null;
		try {
			encode = decoder.decodeBuffer(x);
		} catch (IOException e) {
			log.error("Decryption message error:" + e.getMessage());
			return null;
		}
		return new String(encode);

	}

	/**
	 * base64 encryption
	 * 
	 * @param x
	 *            encryption message
	 * @return
	 */
	public static String base64Encoder(String x) {
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(x.getBytes());
	}
}
 

你可能感兴趣的:(解决java ZipInputStream 读取中文内容的乱码)