日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

commons compress使用+ziji

發布時間:2023/12/14 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 commons compress使用+ziji 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

此文介紹apache的壓縮包,好處是可以自己設置ZipFile文件名的編碼.

而jdk自帶的ZipFIle只支持utf-8,導致壓縮后中文出現亂碼.

?

*下面給出個zip壓縮解壓的示例

---zip壓縮

private void doZip() throws ArchiveException, IOException{ZipArchiveOutputStream zos =(ZipArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("zip", new FileOutputStream(path)); //or new ZipArchiveOutputStream(new FileOutputStream(path)) zos.setEncoding(encoding);String rootPath =FileUtil.getRootPath(files); //獲取要壓縮文件根路徑ZipArchiveEntry ze;for(File f:files){if(!f.exists())continue;ze =new ZipArchiveEntry(getEntryName(f,rootPath)); //獲取每個文件相對路徑,作為在ZIP中路徑zos.putArchiveEntry(ze);//folderif(ze.isDirectory()){zos.closeArchiveEntry();continue;}//fileFileInputStream fis =new FileInputStream(f);IOUtils.copy(fis, zos, BUF);fis.close();zos.closeArchiveEntry();}zos.close(); }private String getEntryName(File f,String rootPath){String entryName;String fPath =f.getAbsolutePath();if(fPath.indexOf(rootPath)!=-1)entryName =fPath.substring(rootPath.length()+1);elseentryName =f.getName();if(f.isDirectory())entryName +="/";   //"/"后綴表示entry是文件夾return entryName; }

?

---zip解壓

public void doZip() throws IOException, ArchiveException{ZipFile file =new ZipFile(sPath,encoding);Enumeration<ZipArchiveEntry> en =file.getEntries();ZipArchiveEntry ze;while(en.hasMoreElements()){ze =en.nextElement();File f =new File(tPath,ze.getName());//創建完整路徑if(ze.isDirectory()){ f.mkdirs();continue;}elsef.getParentFile().mkdirs(); InputStream is =file.getInputStream(ze);OutputStream os =new FileOutputStream(f);IOUtils.copy(is, os, BUF);is.close();os.close();}file.close(); }//這是更一般的解壓處理 public void doTar() throws IOException{TarArchiveInputStream tis =new TarArchiveInputStream(new FileInputStream(sPath));TarArchiveEntry te =null;while((te=tis.getNextTarEntry())!=null){File target =new File(tPath,te.getName());if(te.isDirectory()){target.mkdirs();continue;}elsetarget.getParentFile().mkdirs();FileOutputStream fos =new FileOutputStream(target); //將當前entry寫入文件byte[] buf =new byte[BUF];int len;while((len=tis.read(buf))!=-1){fos.write(buf, 0, len);}fos.close();}tis.close(); }

?

*tar包

---默認tar中entry的長度限制是TarConstants.NAMELEN = 100 [0x64]

原因有待研究?

這個長度應當=操作系統支持的最小長度.

---長文件名處理setLongFileMode(int longFileMode)

參數

/** Fail if a long file name is required in the archive. */
public static final int LONGFILE_ERROR = 0;

/** Long paths will be truncated in the archive. */
public static final int LONGFILE_TRUNCATE = 1;

/** GNU tar extensions are used to store long file names in the archive. */
public static final int LONGFILE_GNU = 2;

在putArchiveEntry()中根據上面的設置處理文件名.

使用長文件名的設置缺點是在其他非gnu操作系統上可能無法解壓

"If you choose the GNU tar option, the archive can not be extracted using many other tar implementations like the ones of OpenBSD, Solaris or MacOS X"

---注意

"The tar package does not support the full POSIX tar standard nor more modern GNU extension of said standard. It cannot deal with entries larger than 2 GByte either. "

?

*Jar包

“Note that ArchiveStreamFactory doesn't distinguish ZIP archives from JAR archives, so if you use the one-argument createArchiveInputStream? method on a JAR archive, it will still return the more generic ZipArchiveInputStream.

The JarArchiveEntry class contains fields for certificates and attributes that are planned to be supported in the future but are not supported as of Compress 1.0.”

從源碼可知JarArchive*Stream和JarArchiveEntry繼承自對應的Zip,從說明可知,目前兩者功能是一致的

package com.me;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;

public class UnZip {
?
?private static final String path = "D://WorkSpace//111.zip";
?
?private static final String tpath = "D://WorkSpace//";
?
?public static void main(String[] args) throws IOException
?{
??//File file = new File(path);
??ZipFile zipFile = new ZipFile(path);
??Enumeration<ZipArchiveEntry> en = zipFile.getEntries();
??ZipArchiveEntry ze;
??while(en.hasMoreElements())
??{
???ze = en.nextElement();
???File file = new File(tpath,ze.getName());
???//創建完整路徑
???if(ze.isDirectory())
???{
????file.mkdirs();
????continue;
???}else{
????file.getParentFile().mkdirs();
???}
???InputStream is =zipFile.getInputStream(ze);
???OutputStream os =new FileOutputStream(file);
???IOUtils.copy(is, os);
???is.close();
???os.close();
??}
??zipFile.close();??
?}
}

總結

以上是生活随笔為你收集整理的commons compress使用+ziji的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。