導航:首頁 > 文件處理 > java壓縮流

java壓縮流

發布時間:2022-01-20 10:27:46

java中zip壓縮輸入輸出流的問題,ZipEntry()方法里的參數到底什麼意思

ZipEntry 用於保存一些被壓縮文件的信息,如文件名,最後訪問時間,最後修改時間,創建時間,文件大小,crc 校驗值 等信息。

ZipEntry 具有一個帶 String 類型參數的構造方法:ZipEntry(String name), name 是入口名稱,就是打開壓縮文件時,看到的裡面的文件名稱。

可以看一下它的源碼,下面是部分源碼:

public
,Cloneable{

Stringname;//entryname
longtime=-1;//lastmodificationtime
FileTimemtime;//lastmodificationtime,fromextrafielddata
FileTimeatime;//lastaccesstime,fromextrafielddata
FileTimectime;//creationtime,fromextrafielddata
longcrc=-1;//crc-32ofentrydata
longsize=-1;//uncompressedsizeofentrydata
longcsize=-1;//compressedsizeofentrydata
intmethod=-1;//compressionmethod
intflag=0;//generalpurposeflag
byte[]extra;//
Stringcomment;//optionalcommentstringforentry

...
}

② 如何用java讀到正在壓縮的壓縮包(使用zip的不壓縮模式)的流

你要知道對應壓縮文件的解碼方式,所謂打開壓縮文件,就是相當於解壓縮,然後放到一個地方
你如果能自己實現對應的壓縮方法,很容易辦到

③ java 傳遞壓縮文件用什麼流

你可以用GZIPInputStream和GZIPOutputStream ,這是專門用來讀寫壓縮流的

④ 你好,最近我也遇到用java壓縮和解壓,向你咨詢下你的解決方案什麼,你是怎麼用文件流方式去壓縮

package com.onewaveinc.cwds.commons.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author fz 2010-7-30
* @Description 把指定文件夾下的所有文件壓縮為指定文件夾下指定zip文件;把指定文件夾下的zip文件解壓到指定目錄下
*/
public class ZipUtils {

private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);

private static final String SEPARATE = "/";

/**
* @Author fz 2010-7-30
* @param sourceDir 待壓縮目錄
* @param zipFile 壓縮文件名稱
* @throws Exception
* @Description 把sourceDir目錄下的所有文件進行zip格式的壓縮,保存為指定zip文件
*/
public static void zip(String sourceDir, String zipFile) throws Exception {
OutputStream os = null;
// try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;

if (file.isDirectory()) {
basePath = file.getPath();
} else {
// 直接壓縮單個文件時,取父目錄
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
// } catch (Exception e) {
// logger.error("壓縮文件或文件夾" + sourceDir + "時發生異常");
// e.printStackTrace();
// }

}

/**
* @Author fz 2010-7-30
* @param source 源文件
* @param basePath 待壓縮文件根目錄
* @param zos 文件壓縮流
* @Description 執行文件壓縮成zip文件
*/
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

//存相對路徑(相對於待壓縮的根目錄)
String pathName = null;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + SEPARATE;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream 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);
}
is.close();
}
}
} catch (Exception e) {
logger.error("壓縮文件" + source + "時發生異常");
e.printStackTrace();
}
}

/**
* @Author fz 2010-7-30
* @param zipfile 待解壓文件
* @param destDir 解壓文件存儲目錄
* @throws Exception
* @Description 解壓zip文件,只能解壓zip文件
*/
@SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir) throws Exception {
destDir = destDir.endsWith(SEPARATE) ? destDir : destDir + SEPARATE;
byte b[] = new byte[1024];
int length;
ZipFile zipFile;
// try {
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists()) {
loadFile.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
outputStream.close();
inputStream.close();
}
}
zipFile.close();
// } catch (IOException e) {
// logger.error("解壓文件" + zipfile + "時發生異常");
// e.printStackTrace();
// }
}

}

⑤ java 壓縮流 指定兩個文件 壓縮到一個壓縮文件上,結果兩個文件的內容 都寫到了 第二個文件上

for (String string : list)
{
BufferedReader in=new BufferedReader(new FileReader(string));
int i=0;
zout.putNextEntry(new ZipEntry(string));
while((i=in.read())!=-1)
{
out.write(i);
}
out.flush();//------------------------------->強制輸出緩沖區
in.close();
}
out.close();

⑥ java 以流的形式解壓帶密碼的zip

可以使用 Runtime 直接調用 winRar 的命令行命令來解壓縮
注意:
1、winRar命令使用,在dos下輸入 unrar 就可以看到全部的命令說明。該命令在winRar的安裝目錄下
2、winRar命令行命令的路徑問題,也就是path。要麼加入系統變數path中,要麼在winRar的安裝目錄下執行程序
以下是程序代碼,解壓 test.rar 到當前目錄下,密碼123

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestRunTime {

public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("unrar e test.rar -p123");//執行解壓縮命令
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;

while ((lineStr = inBr.readLine()) != null)
System.out.println(lineStr);
// 檢查命令是否執行失敗。
if (p.waitFor() != 0) {
if (p.exitValue() == 1)// p.exitValue()==0表示正常結束,1:非正常結束
System.err.println("命令執行失敗!");
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

⑦ java Zip壓縮輸入輸出流問題

帶目錄結構的壓縮,以方便解壓時得到原目錄結構來存放。

⑧ java DeflaterInputStream 壓縮 輸入流

有一個你就讀取一個,循環讀取就行了。

⑨ Java中壓縮輸出流的問題

http://blog.csdn.net/hanshileiai/article/details/6718375
這里剛好有個例子

⑩ java後台怎麼接收一個gzip壓縮流,並且解析接受參數

原則上,不需要在代碼中處理zip只接收就可以。解析可以按HTTP協議自己解析,也可以使用WEB容器完成

閱讀全文

與java壓縮流相關的資料

熱點內容
聯想小新電腦怎麼解壓文件在哪裡 瀏覽:225
while語句java 瀏覽:358
銀行加密貨幣儲蓄 瀏覽:461
map默認參數python 瀏覽:423
山西性能優良壓縮機 瀏覽:197
歐洲tcc編程式模式公司 瀏覽:984
app的股權與投資怎麼寫 瀏覽:580
php網站首頁文件 瀏覽:585
工商銀行app怎麼支持兩個手機登錄 瀏覽:106
安卓手機如何搭建ssr伺服器 瀏覽:668
文件解壓到一半跳出來需要密碼 瀏覽:892
phpmysql編譯pdo 瀏覽:514
怎麼查看公司郵箱伺服器地址 瀏覽:571
php可忽略大小寫的模式符 瀏覽:947
白熊app稿費怎麼樣 瀏覽:464
新建的命令為 瀏覽:426
數組兩種查找演算法集合 瀏覽:757
dw怎麼將源碼拆成幾個文件 瀏覽:231
驗演算法復核法 瀏覽:997
電腦管機就刪除文件夾 瀏覽:481