導航:首頁 > 編程語言 > javazip

javazip

發布時間:2022-02-27 01:53:09

java處理zip和rar的區別

rar 和 zip 的演算法不一樣。
rar是付費產品,演算法沒有公開。

如果想壓縮解壓rar格式,
1.研究一下。winrar的命令
2.用java (Runtime.getRuntime().exec(cmd);)調用命令行來執行。

這個不能在沒有安裝winrar的機器上運行。

⑵ java zip解壓

如果把out.close()寫在注釋處,那麼意味著while循環中創建的out輸出流對象沒有關閉,要知道,如果這個流沒有關閉,那麼該流緩沖區中的數據不會被刷新到實際目標文件中。
因此只有最後一個文件有內容(因為out被關閉時指向最後一個文件)。

⑶ java 如果zip裡面含有zip應該如何將兩個zip都解壓

void decompression(File zipFile){
1:解壓zipFile
2:遍歷解壓後的文件目錄,找到其中的zip文件,然後
decompression(zip文件)
}
就是說遞歸

⑷ java解壓zip文件

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;

/**
* 獲得zip文件里的所有文件
* @author Administrator
*
*/
public class ZipFile {

public ZipFile() throws IOException
{
java.util.zip.ZipFile zf = new java.util.zip.ZipFile("E:/Java/Project.zip");
Enumeration e = zf.entries();
while(e.hasMoreElements())
{
ZipEntry ze = (ZipEntry) e.nextElement();
if(!ze.isDirectory())
System.out.println(new String(ze.getName().getBytes("ISO-8859-1"), "GB2312"));
}
}
public static void main(String[] args) {
try {
new ZipFile();
} catch (IOException e) {
e.printStackTrace();
}
}

}

⑸ java怎麼讀取Zip和RAR裡面的文件啊

ZipInputStream是一個指向ZIP文件的流,這個流最重要的方法就是getNextEntry方法,一個zip文件可以包含好幾個被壓縮的文件,這個方法的功能就是返回下一個目錄項,也就是返回zip文件中的下一項,並且把流指向這個目錄文件項。getNextEntry的返回值是ZipEntry,它表示zip文件中的一個項,它可以返回這個文件項的大小、名稱等。你可以根據它返回的文件大小調用ZipInputStream的read方法來讀取需要的位元組。給你一個例子:public class ZipTest {
public static void main(String args[]) throws FileNotFoundException, IOException{
ZipInputStream zis = new ZipInputStream(new FileInputStream ("c://a.zip"));//生成讀取ZIP文件的流
ZipEntry ze = zis.getNextEntry();//取得下一個文件項
long size = ze.getSize();//取得這一項的大小
FileOutputStream fos = new FileOutputStream("c://"+ze.getName());//產生輸出文件對象
for(int i= 0;i<size;i++){//循環讀取文件並寫入輸出文件對象
byte c = (byte)zis.read();
fos.write(c);
}
fos.close();
zis.close();
}
}

⑹ 怎樣用Java生成ZIP文件

寫了一個 樓主試試吧
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip {

public static void main(String[] args) {
String srcFilePath = "f:/sql.txt";
String zipFilePath = "f:/outfile.zip";

zipFile(srcFilePath, zipFilePath);
}

/**
* 壓縮文件
* @param srcFilePath 需要壓縮的文件的完整路徑
* @param zipFilePath 壓縮生成的文件的路徑
* */
private static void zipFile(String srcFilePath, String zipFilePath) {
if(srcFilePath == null){
throw new RuntimeException("需要壓縮的文件的完整路徑 不能為空!");
}
if(zipFilePath == null){
throw new RuntimeException("壓縮生成的文件的路徑 不能為空!");
}

ZipOutputStream zout = null;
FileInputStream fin = null;

try{
File txtFile = new File(srcFilePath);
fin = new FileInputStream(txtFile);
}catch (FileNotFoundException e) {
throw new RuntimeException("壓縮失敗!找不到文件" + srcFilePath);
}finally {
try {
fin.close();
} catch (Exception e) {

}
}

try {
zout = new ZipOutputStream(new FileOutputStream(new File(zipFilePath)));

File srcFile = new File(srcFilePath);
fin = new FileInputStream(srcFile);

byte[] bb = new byte[4096];
int i = 0;
zout.putNextEntry(new ZipEntry(srcFile.getName()));
while ((i = fin.read(bb)) != -1) {
zout.setLevel(9);
zout.write(bb, 0, i);
}
} catch (IOException e) {
throw new RuntimeException("壓縮失敗!", e);
} finally {
try {
zout.close();
fin.close();
} catch (Exception e) {

}
}
}

}

⑺ java 如何將 txt 文件 變成zip壓縮文件 求例子!!

這個要用 壓縮流類 ZipOutputStream
下面是一個例子 在D盤下有個 名字叫 demo.txt的文件.程序運行後會再D盤下生成一個demo.zip的文件,以下是代碼:
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamDemo {
public static void main(String args[]) throws IOException {
//定義要壓縮的文件 也就是說在D盤里有個 demo.txt 的文件(必須要有,否者會有異常,實際應用中可判斷);
File file = new File("d:" + File.separator + "demo.txt");

//定義壓縮文件的名稱
File zipFile = new File("d:" + File.separator + "demo.zip");

//定義輸入文件流
InputStream input = new FileInputStream(file);

//定義壓縮輸出流
ZipOutputStream zipOut = null;

//實例化壓縮輸出流,並制定壓縮文件的輸出路徑 就是D盤下,名字叫 demo.zip
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));

zipOut.putNextEntry(new ZipEntry(file.getName()));

//設置注釋
zipOut.setComment("www.demo.com");
int temp = 0;
while((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.close();

}
}
希望能幫助樓主,建議樓主多看看JDK文檔,設計到文件的輸出什麼都在JAVA.IO包里,好好看看..
不過樓主要知道,壓縮流也是inputstream和outputstream的子類,但是並沒有定義在java.io包里,而是以一個工具類的形式出現的,但是在用的時候還是需要java.io包的支持的...

⑻ 如何使用java壓縮文件夾成為zip包

在JDK中有一個zip工具類:

java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.

使用此類可以將文件夾或者多個文件進行打包壓縮操作。

在使用之前先了解關鍵方法:

ZipEntry(String name) Creates a new zip entry with the specified name.

使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
/**
*將文件夾下面的文件
*打包成zip壓縮文件
*
*@authoradmin
*
*/
publicfinalclassFileToZip{

privateFileToZip(){}

/**
*將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
*@paramsourceFilePath:待壓縮的文件路徑
*@paramzipFilePath:壓縮後存放路徑
*@paramfileName:壓縮後文件的名稱
*@return
*/
publicstaticbooleanfileToZip(StringsourceFilePath,StringzipFilePath,StringfileName){
booleanflag=false;
FilesourceFile=newFile(sourceFilePath);
FileInputStreamfis=null;
BufferedInputStreambis=null;
FileOutputStreamfos=null;
ZipOutputStreamzos=null;

if(sourceFile.exists()==false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try{
FilezipFile=newFile(zipFilePath+"/"+fileName+".zip");
if(zipFile.exists()){
System.out.println(zipFilePath+"目錄下存在名字為:"+fileName+".zip"+"打包文件.");
}else{
File[]sourceFiles=sourceFile.listFiles();
if(null==sourceFiles||sourceFiles.length<1){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"裡面不存在文件,無需壓縮.");
}else{
fos=newFileOutputStream(zipFile);
zos=newZipOutputStream(newBufferedOutputStream(fos));
byte[]bufs=newbyte[1024*10];
for(inti=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntryzipEntry=newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis=newFileInputStream(sourceFiles[i]);
bis=newBufferedInputStream(fis,1024*10);
intread=0;
while((read=bis.read(bufs,0,1024*10))!=-1){
zos.write(bufs,0,read);
}
}
flag=true;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//關閉流
try{
if(null!=bis)bis.close();
if(null!=zos)zos.close();
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
returnflag;
}

publicstaticvoidmain(String[]args){
StringsourceFilePath="D:\TestFile";
StringzipFilePath="D:\tmp";
StringfileName="12700153file";
booleanflag=FileToZip.fileToZip(sourceFilePath,zipFilePath,fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}

}

⑼ 如何使用java壓縮文件夾成為zip包(最簡單的

import java.io.File;

public class ZipCompressorByAnt {

private File zipFile;

/**
* 壓縮文件構造函數
* @param pathName 最終壓縮生成的壓縮文件:目錄+壓縮文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile);
}

/**
* 執行壓縮操作
* @param srcPathName 需要被壓縮的文件/文件夾
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+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();
}

}

public class TestZip {

public static void main(String[] args) {

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");
zca.compressExe("E:\test1");
}

}

/*如果 出現ant 的 52 51 50 等版本問題 可以去找對應的ant-1.8.2.jar 我開始用的ant-1.10.1.jar 就是這個包版本高了 一直報verson 52 版本問題*/

⑽ java中zip壓縮和gzip壓縮的區別

一個zip可以內藏多個文件
狹義的gzip僅對單個文件壓縮,不能打包多個文件。
tar.gzip或tgz可以打包多個文件,屬於固實壓縮,壓縮比較高,但隨機存取單個文件的效率不如zip..

閱讀全文

與javazip相關的資料

熱點內容
恆智天成資料軟體加密狗怎麼安裝 瀏覽:876
智慧島應用如何下載app 瀏覽:438
java讀取property 瀏覽:552
編譯原理三元式和四元式的書寫 瀏覽:494
蘇寧名校程序員 瀏覽:812
監控分站廠家加密合法嗎 瀏覽:603
蘋果給安卓發簡訊怎麼才能不花錢 瀏覽:658
怎麼讓應用能有文件夾 瀏覽:97
酷我mv有加密嗎 瀏覽:234
分時黃金分割主圖源碼 瀏覽:80
如何退出登錄的伺服器 瀏覽:673
為什麼英雄聯盟伺服器早上就卡 瀏覽:370
程序員手機卡頓解決辦法 瀏覽:111
ios系統轉安卓app如何轉 瀏覽:957
手游eve為什麼連接不到伺服器 瀏覽:370
redis命令執行不了 瀏覽:479
移動程序員p7薪資 瀏覽:92
土壤溫濕演算法 瀏覽:587
現在的編譯器都支持c11 瀏覽:939
安卓手機提醒系統更新怎麼辦 瀏覽:792