您的位置:首页 > 编程语言 > Java开发

读取zip,过滤中文名称 分类: java 2009-11-06 22:46 385人阅读 评论(0) 收藏

2009-11-06 22:46 447 查看
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

FileOutputStream fos = null;
try{
//读取到本地硬盘中
fos = new FileOutputStream(new File("e://icon.zip"));
fos.write(bt);
fos.flush();
fos.close();

//读取mobile2.zip中的内容到images/background/mobile2
ZipFile zfile = new ZipFile("e://icon.zip");
Enumeration zList = zfile.entries();

byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ZipEntry ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
continue;
}

String icon_name = CharSet.toGB2312(ze.getName());
boolean isChinese = false;
byte[] b = icon_name.getBytes();
for(int i=0;i<b.length;i++){
//判断图标名称是否含有中文,只要含有中文,就进行下一轮循环。b[i]=0时时中文,b[i]=63时是'?'
if(b[i]<0 || b[i]==63){
isChinese = true;
break;
}
}
if(isChinese){
continue;
}

// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os = new BufferedOutputStream(new FileOutputStream(getServletContext().getRealPath("/")+"images//background//mobile2//"+icon_name));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf)) != -1) {
os.write(buf, 0, readLen);
}

os.close();
is.close();
}
zfile.close();
}catch(Exception e){
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐