導航:首頁 > 編程語言 > javazip壓縮率

javazip壓縮率

發布時間:2023-05-08 00:21:29

java解壓縮ZIP包問題:

我試了一下,沒有問題
先問一下,你用的JDK是什麼版 本。我是1.6_20,直接用你專的程序
zipFile = new ZipFile(new File(zipfile),"GBK");
Enumeration enumeration = zipFile.getEntries();
是報錯的。
我改成了
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.entries();

這應該屬不是主要問題。

有沒有可能是你的壓縮包損壞了。或是包里的那個文件壞了,跟一下斷點,看一下是解那個文件出的錯。

Ⅱ java 什麼演算法壓縮文件最小

有三種方式實現java壓縮:

1、jdk自帶的包java.util.zip.ZipOutputStream,不足之處,文件(夾)名稱帶中文時,出現亂碼問題,實現代碼如下:
/**
* 功能:把 sourceDir 目錄下的所有文件進行 zip 格式的壓縮,保存為指定 zip 文件
* @param sourceDir 如果是目錄,eg:D:\\MyEclipse\\first\\testFile,則壓縮目錄下所有文件;
* 如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,則只壓縮本文件
* @param zipFile 最後壓縮的文件路徑和名稱,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
*/
public File doZip(String sourceDir, String zipFilePath) throws IOException {
File file = new File(sourceDir);
File zipFile = new File(zipFilePath);
ZipOutputStream zos = null;
try {
// 創建寫出流操作
OutputStream os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);

String basePath = null;

// 獲取目錄
if(file.isDirectory()) {
basePath = file.getPath();
}else {
basePath = file.getParent();
}

zipFile(file, basePath, zos);
}finally {
if(zos != null) {
zos.closeEntry();
zos.close();
}
}

return zipFile;
}
/**
* @param source 源文件
* @param basePath
* @param zos
*/
private void zipFile(File source, String basePath, ZipOutputStream zos)
throws IOException {
File[] files = null;
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

InputStream is = null;
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try{
for(File file : files) {
if(file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
}else {
pathName = file.getPath().substring(basePath.length() + 1);
is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
}
}
}finally {
if(is != null) {
is.close();
}
}
}

2、使用org.apache.tools.zip.ZipOutputStream,代碼如下,
package net.szh.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipCompressor {
static final int BUFFER = 8192;

private File zipFile;

public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}

public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void compress(File file, ZipOutputStream out, String basedir) {
/* 判斷是目錄還是文件 */
if (file.isDirectory()) {
System.out.println("壓縮:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("壓縮:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}

/** 壓縮一個目錄 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;

File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 遞歸 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}

/** 壓縮一個文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

3、可以用ant中的org.apache.tools.ant.taskdefs.Zip來實現,更加簡單。
package net.szh.zip;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class ZipCompressorByAnt {

private File zipFile;

public ZipCompressorByAnt(String pathName) {
zipFile = new File(pathName);
}

public void compress(String srcPathName) {
File srcdir = new File(srcPathName);
if (!srcdir.exists())
throw new RuntimeException(srcPathName + "不存在!");

Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); 排除哪些文件或文件夾
zip.addFileset(fileSet);

zip.execute();
}
}
測試一下
package net.szh.zip;

public class TestZip {
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("E:\\szhzip.zip");
zc.compress("E:\\test");

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");
zca.compress("E:\\test");
}
}

Ⅲ java 壓縮比

ZipOutputStream

裡面有
setMethodpublic void setMethod(int method)設置用於後續條目的默認壓縮方法。只要沒有為單個 ZIP 文件條目指定壓縮方法,並且其初始設置為 DEFLATED 時,就使用此默認值。

參數:method - 默認壓縮方法
拋出:IllegalArgumentException - 如果指定的壓縮方法無效
setLevelpublic void setLevel(int level)為後續的 DEFLATED 條目設置壓縮級別。默認設置是 DEFAULT_COMPRESSION。

參數:level - 壓縮級別 (0-9)
拋出:IllegalArgumentException - 如果壓縮級別無效

Ⅳ java如何直接解壓zip格式二進制流

Java代碼
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

class ZipTest {
// 壓縮
public static void zip(String zipFileName, String inputFile)
throws Exception {
File f = new File(inputFile);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, f, f.getName());
System.out.println("zip done");
out.close();

Ⅳ java中的壓縮原理是什麼

什麼是壓縮文件?
簡單的說,就是經過壓縮軟體壓縮的文件叫壓縮文件,壓縮的原理是把文件的二進制代碼壓縮,把相鄰的0,1代碼減少,比如有000000,可以把它變成6個0
的寫法60,來減少該文件的空間。
■怎麼壓縮文件?
首先要安裝壓縮軟體,現在比較流行的是WinRAR「一種高效快速的文件壓縮軟體(中文版)」。
其次是建立一個壓縮包:選擇你要製作成壓縮包的文件或文件夾,當然你也可也多選,方法同資源管理器,也就是按住Ctrl或Shift再選擇文件(文件夾)。
選取完畢之後,就可以單擊工具欄上的「壓縮」按鈕,在這里你可以選擇壓縮格式:RAR和ZIP。
如果你想得到較大的壓縮率,建議選擇RAR格式。
各個選項選擇好以後,單擊確定按鈕就開始製作壓縮包了,非常方便。有時候大家會遇到這個問題,就是你在一個論壇里要上傳一些文件壓縮包,壓縮包大小有3M,但是論壇限制會員上傳大小隻有2M,怎麼辦呢?
其實辦法很簡單,就是在你壓縮這個文件時,分成幾個帶分卷壓縮包,分卷包大小設置為2M即可,比如:原來文件名為123.rar(3M),壓縮成分卷包後為123.part1.rar(2M)與123.part2.rar(1M)兩個文件,這樣你就可以上傳了。
具體方法如下:
1、在要壓縮的文件上點右鍵
2、添加到壓縮文件....
3、選常規
4、壓縮方式選最好
5、批定壓縮分卷大小(按位元組計算),1M
=
1024K,1K
=
1024位元組,填寫數字即可
當你下載了帶有分卷的壓縮包後,如何解壓文件呢?
具體方法如下:
1、把所有的壓縮分卷全部下載完整
2、所有分卷必須在同一個文件夾內
3、然後雙擊解壓第一個分卷,即可
註:分卷解壓的文件必須是連續的,若分卷未下載完整,則解壓時自然會提示需要下一壓縮分卷

Ⅵ 怎樣用java快速實現zip文件的壓縮解壓縮

packagezip;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
importorg.apache.commons.lang3.StringUtils;
publicclassZipUtil{

/**
*遞歸壓縮文件夾
*@paramsrcRootDir壓縮文件夾根目錄的子路徑
*@paramfile當前遞歸壓縮的文件或目錄對象
*@paramzos壓縮文件存儲對象
*@throwsException
*/
privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException
{
if(file==null)
{
return;
}

//如果是文件,則直接壓縮該文件
if(file.isFile())
{
intcount,bufferLen=1024;
bytedata[]=newbyte[bufferLen];

//獲取文件相對於壓縮文件夾根目錄的子路徑
StringsubPath=file.getAbsolutePath();
intindex=subPath.indexOf(srcRootDir);
if(index!=-1)
{
subPath=subPath.substring(srcRootDir.length()+File.separator.length());
}
ZipEntryentry=newZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));
while((count=bis.read(data,0,bufferLen))!=-1)
{
zos.write(data,0,count);
}
bis.close();
zos.closeEntry();
}
//如果是目錄,則壓縮整個目錄
else
{
//壓縮目錄中的文件或子目錄
File[]childFileList=file.listFiles();
for(intn=0;n<childFileList.length;n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir,childFileList[n],zos);
}
}
}

/**
*對文件或文件目錄進行壓縮
*@paramsrcPath要壓縮的源文件路徑。如果壓縮一個文件,則為該文件的全路徑;如果壓縮一個目錄,則為該目錄的頂層目錄路徑
*@paramzipPath壓縮文件保存的路徑。注意:zipPath不能是srcPath路徑下的子文件夾
*@paramzipFileName壓縮文件名
*@throwsException
*/
publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException
{
if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStreamcos=null;
ZipOutputStreamzos=null;
try
{
FilesrcFile=newFile(srcPath);

//判斷壓縮文件保存的路徑是否為源文件路徑的子文件夾,如果是,則拋出異常(防止無限遞歸壓縮的發生)
if(srcFile.isDirectory()&&zipPath.indexOf(srcPath)!=-1)
{
thrownewParameterException(ICommonResultCode.INVALID_PARAMETER,".");
}

//判斷壓縮文件保存的路徑是否存在,如果不存在,則創建目錄
FilezipDir=newFile(zipPath);
if(!zipDir.exists()||!zipDir.isDirectory())
{
zipDir.mkdirs();
}

//創建壓縮文件保存的文件對象
StringzipFilePath=zipPath+File.separator+zipFileName;
FilezipFile=newFile(zipFilePath);
if(zipFile.exists())
{
//檢測文件是否允許刪除,如果不允許刪除,將會拋出SecurityException
=newSecurityManager();
securityManager.checkDelete(zipFilePath);
//刪除已存在的目標文件
zipFile.delete();
}

cos=newCheckedOutputStream(newFileOutputStream(zipFile),newCRC32());
zos=newZipOutputStream(cos);

//如果只是壓縮一個文件,則需要截取該文件的父目錄
StringsrcRootDir=srcPath;
if(srcFile.isFile())
{
intindex=srcPath.lastIndexOf(File.separator);
if(index!=-1)
{
srcRootDir=srcPath.substring(0,index);
}
}
//調用遞歸壓縮方法進行目錄或文件壓縮
zip(srcRootDir,srcFile,zos);
zos.flush();
}
catch(Exceptione)
{
throwe;
}
finally
{
try
{
if(zos!=null)
{
zos.close();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

/**
*解壓縮zip包
*@paramzipFilePathzip文件的全路徑
*@paramunzipFilePath解壓後的文件保存的路徑
*@paramincludeZipFileName解壓後的文件保存的路徑是否包含壓縮文件的文件名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
publicstaticvoinzip(StringzipFilePath,StringunzipFilePath,booleanincludeZipFileName)throwsException
{
if(StringUtils.isEmpty(zipFilePath)||StringUtils.isEmpty(unzipFilePath))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
FilezipFile=newFile(zipFilePath);
//如果解壓後的文件保存路徑包含壓縮文件的文件名,則追加該文件名到解壓路徑
if(includeZipFileName)
{
StringfileName=zipFile.getName();
if(StringUtils.isNotEmpty(fileName))
{
fileName=fileName.substring(0,fileName.lastIndexOf("."));
}
unzipFilePath=unzipFilePath+File.separator+fileName;
}
//創建解壓縮文件保存的路徑
FileunzipFileDir=newFile(unzipFilePath);
if(!unzipFileDir.exists()||!unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}

//開始解壓
ZipEntryentry=null;
StringentryFilePath=null,entryDirPath=null;
FileentryFile=null,entryDir=null;
intindex=0,count=0,bufferSize=1024;
byte[]buffer=newbyte[bufferSize];
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
ZipFilezip=newZipFile(zipFile);
Enumeration<ZipEntry>entries=(Enumeration<ZipEntry>)zip.entries();
//循環對壓縮包里的每一個文件進行解壓
while(entries.hasMoreElements())
{
entry=entries.nextElement();
//構建壓縮包中一個文件解壓後保存的文件全路徑
entryFilePath=unzipFilePath+File.separator+entry.getName();
//構建解壓後保存的文件夾路徑
index=entryFilePath.lastIndexOf(File.separator);
if(index!=-1)
{
entryDirPath=entryFilePath.substring(0,index);
}
else
{
entryDirPath="";
}
entryDir=newFile(entryDirPath);
//如果文件夾路徑不存在,則創建文件夾
if(!entryDir.exists()||!entryDir.isDirectory())
{
entryDir.mkdirs();
}

//創建解壓文件
entryFile=newFile(entryFilePath);
if(entryFile.exists())
{
//檢測文件是否允許刪除,如果不允許刪除,將會拋出SecurityException
=newSecurityManager();
securityManager.checkDelete(entryFilePath);
//刪除已存在的目標文件
entryFile.delete();
}

//寫入文件
bos=newBufferedOutputStream(newFileOutputStream(entryFile));
bis=newBufferedInputStream(zip.getInputStream(entry));
while((count=bis.read(buffer,0,bufferSize))!=-1)
{
bos.write(buffer,0,count);
}
bos.flush();
bos.close();
}
}

publicstaticvoidmain(String[]args)
{
StringzipPath="d:\ziptest\zipPath";
Stringdir="d:\ziptest\rawfiles";
StringzipFileName="test.zip";
try
{
zip(dir,zipPath,zipFileName);
}
catch(Exceptione)
{
e.printStackTrace();
}

StringzipFilePath="D:\ziptest\zipPath\test.zip";
StringunzipFilePath="D:\ziptest\zipPath";
try
{
unzip(zipFilePath,unzipFilePath,true);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

Ⅶ java怎麼獲得rar和zip文件的壓縮率

/**
* 計算ZIP文件的解壓大小
*
* @param inputStream 輸入流
* 註:輸入流由調用方負責關閉
* @return 解壓大小
*/
public static long getUncompressedSize(InputStream inputStream) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"));

byte[] buffer = new byte[8*1024*1024];

long uncompressedSize = 0;

ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null) {
long size = zipEntry.getSize();

// ZipEntry的size可能為-1,表示未知
if (size == -1) {
int len;
while ((len = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
uncompressedSize += len;
}
} else {
uncompressedSize += size;
}

zipInputStream.closeEntry();
}

return uncompressedSize;
}
目前只找到這個,zip的,rar的沒找到

Ⅷ java壓縮文件最高壓縮率

更好的壓縮率(使用gzip時具有最佳壓縮級別 LZMA達到38%,bzip2 34% 25%)。 壓縮桐沒帆察閉率的增加主要體現在二進制文件上。 解局雹壓縮時間比bzip2快(3-...

Ⅸ Java做zip壓縮時,磁碟讀寫次數高且速度慢,能不能加個緩沖區解決

試一下 我的 方法 這個 包你滿意。。

public static void zip(File[] files, String outputFile) throws IOException
{
//如果files長度為0,zout.close()方法會拋異常: ZIP file must have at least one entry
if(files.length == 0)
{
LogUtils.warn("Log file list is empty");
return;
}
FileOutputStream out = null;
BufferedOutputStream buffOut = null;
ZipOutputStream zout = null;
try
{
out = new FileOutputStream(outputFile);
buffOut = new BufferedOutputStream(out);
zout = new ZipOutputStream(buffOut);
for (int i = 0; i < files.length; i++)
{
InputStream in = null;
BufferedInputStream buffIn = null;
try
{
in = new FileInputStream(files[i]);
buffIn = new BufferedInputStream(in, BUFF_SIZE);
ZipEntry zipEntry = new ZipEntry(files[i].getName());
zout.putNextEntry(zipEntry);
int len = 0;
byte data[] = new byte[BUFF_SIZE];
while ((len = buffIn.read(data)) != -1)
{
zout.write(data, 0, len);
}
}
finally
{
try
{
zout.closeEntry();
}
catch (IOException e)
{
LogUtils.exception(e, "Close zip file entry failed");
}
closeStream(buffIn);
closeStream(in);
}
}
}
finally
{
closeStream(zout);
closeStream(buffOut);
closeStream(out);
}
}

閱讀全文

與javazip壓縮率相關的資料

熱點內容
桌面雲配置文件分離 瀏覽:505
iphone5如何升級4g網路 瀏覽:5
團購是在哪個app 瀏覽:897
打開多個word文檔圖片就不能顯示 瀏覽:855
騰訊新聞怎麼切換版本 瀏覽:269
app安裝失敗用不了 瀏覽:326
桌面文件滑鼠點開會變大變小 瀏覽:536
手機誤刪系統文件開不了機 瀏覽:883
微信兔子甩耳朵 瀏覽:998
android藍牙傳文件在哪裡 瀏覽:354
蘋果6s軟解是真的嗎 瀏覽:310
c語言代碼量大 瀏覽:874
最新網路衛星導航如何使用 瀏覽:425
以下哪些文件屬於圖像文件 瀏覽:774
zycommentjs 瀏覽:414
確認全血細胞減少看哪些數據 瀏覽:265
文件有哪些要求 瀏覽:484
cad打開時會出現兩個文件 瀏覽:65
什麼是轉基因網站 瀏覽:48
手柄設備有問題代碼43 瀏覽:921

友情鏈接