使用java.util.zip解ZIP文件壓縮,但不只,為何文件名不支持中文,如果那位高手知道,請告訴我,哈哈,謝謝
String compress ="D:/doc_c06_net.zip";//zip壓縮文件
String decompression="D:/doc_redcome_com/";//解壓路徑
java.io.File dir =new java.io.File(decompression);
java.util.zip.ZipFile zf = new java.util.zip.ZipFile(compress);
java.util.Enumeration enumer = zf.entries();
while (enumer.hasMoreElements())
{
java.util.zip.ZipEntry ze = (java.util.zip.ZipEntry) enumer.nextElement();
String zename = ze.getName();
zename = new String(zename.getBytes("ISO-8859-1"), "UTF-8");//文件名,不支持中文.
System.out.println(zename);
if (ze.isDirectory())
{
java.io.File file = new java.io.File(dir.getAbsolutePath() + "/" + zename);
file.mkdirs();
} else
{
java.io.File file = new java.io.File(dir.getAbsolutePath() + "/" + zename).getParentFile();
if (!file.exists())
{
file.mkdirs();
}
byte zeby[] = new byte[(int) ze.getSize()];
java.io.InputStream is = zf.getInputStream(ze);
is.read(zeby);
is.close();
java.io.FileOutputStream fos = new java.io.FileOutputStream(
dir.getAbsolutePath() + "/" + zename);
fos.write(zeby);
fos.close();
}
}
zf.close();
使用java.util.zip,把目錄壓縮為ZIP文件
public static void main(String args[])
{
String compress ="D:/doc_c06_net/";//要壓縮的目錄
String decompression="D:/doc_redcome_com/";//解壓路徑
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(
new java.io.FileOutputStream(相關(guān)文章
>new java.io.File(decompression))); compress(zos,file,""); zos.close(); } public void compress(java.util.zip.ZipOutputStream zos,java.io.File file,String context
)throws java.io.IOException { if(file.isFile()) { byte by[] = new byte[(int) file.length()]; java.io.FileInputStream is = new java.io.FileInputStream(file); is.read(by); is.close(); java.util.zip.ZipEntry ze=new java.util.zip.ZipEntry(context+file.getName()); zos.putNextEntry(ze); zos.write(by); zos.closeEntry(); }else if(file.isDirectory()) { java.io.File files[]=file.listFiles(); if(files!=null) for(int index=0;index<files.length;index++) { compress(zos,files[index],context+"/"+file.getName()+"/"); } } }