package com.zjasm.util;
/**
* Created with IntelliJ IDEA.
* Date: 13-7-31
* Time: 下午4:29
* To change this template use File | Settings | File Templates.
*/
/**
version: 1.1 / 2007-01-25
- changed BOM recognition ordering (longer boms first)
Original pseudocode : Thomas Weidenfeller
Implementation tweaked: Aki Nieminen
http://www.unicode.org/unicode/faq/utf_bom.html
BOMs:
00 00 FE FF = UTF-32, big-endian
FF FE 00 00 = UTF-32, little-endian
EF BB BF = UTF-8,
FE FF = UTF-16, big-endian
FF FE = UTF-16, little-endian
Win2k Notepad:
Unicode format = UTF-16LE
***/
import java.io.*;
/**
* Generic unicode textreader, which will use BOM mark
* to identify the encoding to be used. If BOM is not found
* then use a given default or system encoding.
*/
public class UnicodeReader extends Reader {
PushbackInputStream internalIn;
InputStreamReader internalIn2 = null;
String defaultEnc;
private static final int BOM_SIZE = 4;
/**
*
* @param in inputstream to be read
* @param defaultEnc default encoding if stream does not have
* BOM marker. Give NULL to use system-level default.
*/
UnicodeReader(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultEnc;
}
public String getDefaultEncoding() {
return defaultEnc;
}
/**
* Get stream encoding or NULL if stream is uninitialized.
* Call init() or read() method to initialize it.
*/
public String getEncoding() {
if (internalIn2 == null) return null;
return internalIn2.getEncoding();
}
/**
* Read-ahead four bytes and check for BOM marks. Extra bytes are
* unread back to the stream, only BOM bytes are skipped.
*/
protected void init() throws IOException {
if (internalIn2 != null) return;
String encoding;
byte bom[] = new byte[BOM_SIZE];
int n, unread;
n = internalIn.read(bom, 0, bom.length);
if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
(bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
(bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
(bom[2] == (byte)0xBF) ) {
encoding = "UTF-8";
unread = n - 3;
} else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
encoding = "UTF-16LE";
unread = n - 2;
} else {
// Unicode BOM mark not found, unread all bytes
encoding = defaultEnc;
unread = n;
}
//System.out.println("read=" + n + ", unread=" + unread);
if (unread > 0) internalIn.unread(bom, (n - unread), unread);
// Use given encoding
if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn);
} else {
internalIn2 = new InputStreamReader(internalIn, encoding);
}
}
public void close() throws IOException {
init();
internalIn2.close();
}
public int read(char[] cbuf, int off, int len) throws IOException {
init();
return internalIn2.read(cbuf, off, len);
}
}
package com.zjasm.util;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.*;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* Date: 13-7-8
* Time: 上午10:25
* To change this template use File | Settings | File Templates.
*/
public class ZipUtil {
public static final int BUFFER = 1024*5;
/**
* 可以处理中文文件名
*/
public static List unZip(String path){
List zipList = new ArrayList();
int count = -1;
int index = -1;
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String newFilePath = path.substring(0,path.lastIndexOf(".zip"))+"\\";
new File(newFilePath).mkdirs();
String targetPath = newFilePath;//解压到路径
try{
ZipFile zipFile = new ZipFile(path,"utf-8");//防止中文文件名乱码
Enumeration entries = zipFile.getEntries();
while(entries.hasMoreElements()){
byte buf[] = new byte[BUFFER];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
if (entry.isDirectory()) {
new File(targetPath + filename).mkdirs();
continue;
}
zipList.add(filename);
index = filename.lastIndexOf("/");
if(index > -1){
filename = filename.substring(index+1);
}
filename = targetPath + filename;
file = new File(filename);
file.createNewFile();
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, BUFFER);
while((count = is.read(buf)) > -1){
bos.write(buf, 0, count );
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
return zipList;
}
/**
* 解压zip文件(不支持中文文件名)
*
* @param filePath
* zip文件全路径
* @throws IOException
*/
/*public static List unZip(String filePath)
throws IOException {
List zipList = new ArrayList();
String newFilePath = filePath.substring(0,filePath.lastIndexOf(".zip"))+"\\";
new File(newFilePath).mkdirs();
String targetPath = newFilePath;//解压到路径
ZipFile zipFile = new ZipFile(filePath);
Enumeration emu = zipFile.entries();
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
zipList.add(entry.getName());
if (entry.isDirectory()) {
new File(targetPath + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));
File file = new File(targetPath+entry.getName());
File parent = file.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
return zipList;
}*/
/**
* 动态改变数组
*
*/
public static Object[] insertArr(Object[] arr, Object str){
int size = arr.length;
Object[] tmp = new Object[size + 1];
System.arraycopy(arr, 0, tmp, 0, size);
tmp[size] = str;
return tmp;
}
public static String loadFile(String file) throws IOException {
// read text file, auto recognize bom marker or use
// system default if markers not found.
BufferedReader reader = null;
CharArrayWriter writer = null;
UnicodeReader r = new UnicodeReader(new FileInputStream(file), null);
char[] buffer = new char[16 * 1024]; // 16k buffer
int read;
try {
reader = new BufferedReader(r);
writer = new CharArrayWriter();
while( (read = reader.read(buffer)) != -1) {
writer.write(buffer, 0, read);
}
writer.flush();
return writer.toString();
} catch (IOException ex) {
throw ex;
} finally {
try {
writer.close(); reader.close(); r.close();
} catch (Exception ex) { }
}
}
public String readTxt(String txtPath){
StringBuffer stringBuffer = new StringBuffer();
String line = null;
try {
InputStreamReader read = new InputStreamReader(
new FileInputStream(new File(txtPath)),"utf-8");//考虑到编码格式
BufferedReader br = new BufferedReader(read);
while ((line = br.readLine()) != null) {
stringBuffer.append(line+"\n");
}
br.close();
read.close();
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
}
分享到:
相关推荐
java控制指定路径下的文件夹压缩成zip格式。 java将zip压缩包解压成为文件夹。直接使用io流进行操作,不需借助其他jar包
Java中递归逻辑循环调用解压zip里面所有的压缩包 Java中递归逻辑循环调用解压zip里面所有的压缩包
楼上兄弟太黑,特意找的源码,2分处理,完美解决Java加密解密zip文件
java 打zip压缩包 解压缩包Eclipse项目20111011 java 打zip压缩包 解压缩包Eclipse项目20111011java 打zip压缩包 解压缩包Eclipse项目20111011
java ZIP和RAR 压缩包 目录结构。 1.ant.jar 解决java自带zip不能读取中文压缩包的问题; 2.需要安装WINRAR软件,以便解压rar文件,然后获取对应目录; 3.实现在线预览压缩包目录结构的功能;
使用Java远程上传压缩包到服务器,或者在服务器上拉去压缩包或者文件,在服务器上创建目录。
本篇文章主要介绍了详解Java无需解压直接读取Zip文件和文件内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
完美支持解压zip文件多层文件解压,文件夹,文件,支持中文。
使用java解压缩文件,并获取压缩文件中的文件名称,使用多线程,提高效率
有些时候需要替换zip内的文件。 网上的办法大多是——先解压,然后对解压目录替换文件,最后再重新压缩。该办法需要比较繁琐,且需要一个临时目录。 于是想找无需解压的方案。 后来找到利用 ZipInputStream、...
Java实现的带密码压缩与解压zip文件源码,可将多个文件使用不同的密码压进同一个zip文件;可以解压带密码的zip文件(store压缩模式)
包含了使用的jar包,以及一个Java类,实现了使用Java对多目录多层级的文件进行打包,以及对ZIP包进行解压缩的操作。
Java批量解压RAR或zip文件,压缩包内包含包括源码和jar包。 如发现其他人或组织有商用目的,必追究其法律责任。
Java提取压缩包中的文件,文件操作的范畴,操作对象为ZIP格式的压缩文件,将从ZIP中提取出BMP图像文件,要将从压缩包中提取文件,必须先解压压缩包,然后将指定文件拷贝出来,代码中先实现了对zip文件的解压,然后...
jsp页面上传zip压缩包,用ajax传给后端进行解压,创建路径解压存储在本地,然后程序调用解压后的文件进行操作,然后再删除临时文件夹。用于批量导入信息和照片,然后ajax回调导入结果返回前端显示给操作者。
java 解压zip,rar文件 ,java 解压缩包,把压缩包的文件及文件夹的子文件全部压到指定目录
使用spring boot打造文件文档在线预览项目解决方案,支持doc、docx、ppt、pptx、xls、xlsx、zip、rar、mp4,mp3以及众多类文本如txt、html、xml、java、properties、sql、js、md、json、conf、ini、vue、php、py、...
zip解压缩文件文件夹都可以,代码清晰,注释多。非常容易看懂
使用Java解压文件 支持RAR5,Zip,7z ,代码只实现了这个三种,但是要想支持更多的可以查看代码。 支持跨平台。 以前的通用java解压都是坑人的,无语了。
实现JAVA解压缩带密码的RAR压缩包,支持中文,支持Window、Linux平台