導航:首頁 > 文件處理 > javazip解壓和壓縮

javazip解壓和壓縮

發布時間:2024-08-10 20:19:10

1. java設計 ZIP格式壓縮/解壓系統設計

給你一點關鍵部分的提示
你可能需要用到apache zip這個組件
http://camel.apache.org/zip-dataformat.html

// zipFileName為要解壓縮的zip為文件名,例:c:\\filename.zip
// outputDirectoty為解壓縮後文件名,例:c:\\filename

public void unZip(String zipFileName, String outputDirectory)
throws Exception {
InputStream in = null;
FileOutputStream out = null;
try {
zipFile = new ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
// org.apache.tools.zip.ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("創建目錄:" + outputDirectory
+ File.separator + name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
// System.out.println("測試文件1:" +fileName);
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1, fileName
.length());
}
try {
f = new File(outputDirectory + File.separator
+ zipEntry.getName());

in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);

byte[] by = new byte[100000];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
} catch (Exception ee) {

} finally {
if (in != null) {
in.close(); //解壓完成後注意關閉輸入流對象
}
if (out != null) {
out.close(); //解壓完成後注意關閉輸出流對象
}
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close(); //解壓完成後注意關閉apache自帶zip流對象
}
}

2. 用java小應用程序實現文件壓縮、解壓縮

40.ZIP壓縮文件
/*
import java.io.*;
import java.util.zip.*;
*/
//創建文件輸入流對象
FileInputStream fis=new FileInputStream(%%1);
//創建文件輸出流對象
FileOutputStream fos=new FileOutputStream(%%2);
//創建ZIP數據輸出流對象
ZipOutputStream zipOut=new ZipOutputStream(fos);
//創建指向壓縮原始文件的入口
ZipEntry entry=new ZipEntry(args[0]);
zipOut.putNextEntry(entry);
//向壓縮文件中輸出數據
int nNumber;
byte[] buffer=new byte[1024];
while((nNumber=fis.read(buffer))!=-1)
zipOut.write(buffer,0,nNumber);
//關閉創建的流對象
zipOut.close();
fos.close();
fis.close();
}
catch(IOException e)
{
System.out.println(e);
}

41.獲得應用程序完整路徑
String %%1=System.getProperty("user.dir");

42.ZIP解壓縮
/*
import java.io.*;
import java.util.zip.*;
*/
try{
//創建文件輸入流對象實例
FileInputStream fis=new FileInputStream(%%1);
//創建ZIP壓縮格式輸入流對象實例
ZipInputStream zipin=new ZipInputStream(fis);
//創建文件輸出流對象實例
FileOutputStream fos=new FileOutputStream(%%2);
//獲取Entry對象實例
ZipEntry entry=zipin.getNextEntry();
byte[] buffer=new byte[1024];
int nNumber;
while((nNumber=zipin.read(buffer,0,buffer.length))!=-1)
fos.write(buffer,0,nNumber);
//關閉文件流對象
zipin.close();
fos.close();
fis.close();
}
catch(IOException e) {
System.out.println(e);
}

43.遞歸刪除目錄中的文件
/*
import java.io.*;
import java.util.*;
*/
ArrayList<String> folderList = new ArrayList<String>();
folderList.add(%%1);
for (int j = 0; j < folderList.size(); j++) {
File file = new File(folderList.get(j));
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
f.delete();
}
}

43.ZIP壓縮文件夾
/*
http://findjar.com/index.jsp
import java.io.*;
import org.apache.tools.zip.ZipOutputStream; //這個包在ant.jar里,要到官方網下載
//java.util.zip.ZipOutputStream
import java.util.zip.*;
*/
try {
String zipFileName = %%2; //打包後文件名字
File f=new File(%%1);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
String base= "";
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
out.close();
}catch (Exception ex) {
ex.printStackTrace();
}
/*
切,我剛好寫了個壓縮的,但沒寫解壓的

1. 解壓的(參數兩個,第一個是你要解壓的zip文件全路徑,第二個是你解壓之後要存放的位置)

/*
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
*/
public class ZipFileList {

public static void main(String[] args) {
extZipFileList(args[0],args[1]);
}

private static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
System.out.println("創建文件夾:" + entryName);
} else {
FileOutputStream os = new FileOutputStream(extPlace
+ entryName);
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();

}
}
} catch (IOException e) {
}
System.out.println("解壓文件成功");
}
}

壓縮的(參數最少傳兩個,第一個是你壓縮之後的文件存放的位置以及名字,第二個是你要壓縮的文件或者文件夾所在位置,也可以傳多個文件或文件夾)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFileOther {

public static String zipFileProcess(ArrayList outputZipFileNameList, String outputZipNameAndPath) {
ArrayList fileNames = new ArrayList();
ArrayList files = new ArrayList();
FileOutputStream fileOut = null;
ZipOutputStream outputStream = null;
FileInputStream fileIn = null;
StringBuffer sb = new StringBuffer(outputZipNameAndPath);
// FileInputStream fileIn =null;
try {
if (outputZipNameAndPath.indexOf(".zip") != -1) {
outputZipNameAndPath = outputZipNameAndPath;
} else {
sb.append(".zip");
outputZipNameAndPath = sb.toString();
}
fileOut = new FileOutputStream(outputZipNameAndPath);
outputStream = new ZipOutputStream(fileOut);
int outputZipFileNameListSize = 0;
if (outputZipFileNameList != null) {
outputZipFileNameListSize = outputZipFileNameList.size();
}
for (int i = 0; i < outputZipFileNameListSize; i++) {
File rootFile = new File(outputZipFileNameList.get(i).toString());
listFile(rootFile, fileNames, files, "");
}
for (int loop = 0; loop < files.size(); loop++) {
fileIn = new FileInputStream((File) files.get(loop));
outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop)));
byte[] buffer = new byte[1024];
while (fileIn.read(buffer) != -1) {
outputStream.write(buffer);
}
outputStream.closeEntry();
fileIn.close();
}
return outputZipNameAndPath;
} catch (IOException ioe) {
return null;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
if (fileIn != null) {
try {
fileIn.close();
} catch (IOException e) {
}
}
}
}

public static void main(String[] args) {
ArrayList outputZipFileName=new ArrayList();
String savePath="";
int argSize = 0;
if (args != null) {
argSize = args.length;
}

if (argSize > 1) {
if(args[0]!=null)
savePath = args[0];
for(int i=1;i<argSize;i++){
if(args[i]!=null){
outputZipFileName.add(args[i]);
}
}
ZipFileOther instance=new ZipFileOther();
instance.zipFileProcess(outputZipFileName,savePath);
} else {
}

}

private static void listFile(File parentFile, List nameList, List fileList, String directoryName) {
if (parentFile.isDirectory()) {
File[] files = parentFile.listFiles();
for (int loop = 0; loop < files.length; loop++) {
listFile(files[loop], nameList, fileList, directoryName + parentFile.getName() + "/");
}
} else {
fileList.add(parentFile);
nameList.add(directoryName + parentFile.getName());
}
}
}

*/

3. JAVA怎麼把zip文件解壓到指定位置

剛好我在項目中用到了,送給你,希望你能用上。

/**
* 解壓,處理下載的zip工具包文件
*
* @param directory
* 要解壓到的目錄
* @param zip
* 工具包文件
*
* @throws Exception
* 操作失敗時拋出異常
*/
public static void unzipFile(String directory, File zip) throws Exception
{
try
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
ZipEntry ze = zis.getNextEntry();
File parent = new File(directory);
if (!parent.exists() && !parent.mkdirs())
{
throw new Exception("創建解壓目錄 \"" + parent.getAbsolutePath() + "\" 失敗");
}
while (ze != null)
{
String name = ze.getName();
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0)
{
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
ze = zis.getNextEntry();
}
zis.close();
}
catch (IOException e)
{
}
}

4. 為什麼JAVA軟體壓縮包(ZIP RAR)解壓後有很多後綴class的文件和文件夾,而沒有jar文件

那是java文件的內容,你解壓過頭了,把jar文件也解壓了,那你當然沒法看到jar啊,如果你是解壓zip文件就解壓處class這種文件,建議用文件管理器,改文件的後綴名。如zip的改為jar。這樣應該就可以了安裝了,不需要再解壓了。
希望我的回答對你有用

5. 你好,最近我也遇到用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();
// }
}

}

6. java中將一個文件夾下所有的文件壓縮成一個文件,然後,解壓到指定目錄.

import java.io.*;
import java.util.zip.*;
public class CompressD {
// 緩沖
static byte[] buffer = new byte[2048];
public static void main(String[] args) throws Exception {
// 來源
File inputDir = new File("C:\\CompressTest\\");
// 目標
FileOutputStream fos = new FileOutputStream("C:\\CompressTest.zip");
// 過濾
ZipOutputStream zos = new ZipOutputStream(fos);
// 壓縮
zip(inputDir.listFiles(), "", zos);
// 關閉
zos.close();
}
private static void zip(File[] files, String baseFolder, ZipOutputStream zos)
throws Exception {
// 輸入
FileInputStream fis = null;
// 條目
ZipEntry entry = null;
// 數目
int count = 0;
for (File file : files) {
if (file.isDirectory()) {
// 遞歸
zip(file.listFiles(), file.getName() + File.separator, zos);
continue;
}
entry = new ZipEntry(baseFolder + file.getName());
// 加入
zos.putNextEntry(entry);
fis = new FileInputStream(file);
// 讀取
while ((count = fis.read(buffer, 0, buffer.length)) != -1)
// 寫入
zos.write(buffer, 0, count);
}
}
}

7. 請大神幫忙解決一個用java解壓縮一個zip壓縮格式位元組流中文內容亂碼問題!

這個問題我有點印象,好像是包的問題。好像不能用zip的那個,換另一個包就好了。具體我也不記得了

8. java 解壓zip中文文件保存

用jdk自帶的zip工具做壓縮時,對中文支持不是很好。
建議使用ant.jar中的壓縮和解壓縮工具

閱讀全文

與javazip解壓和壓縮相關的資料

熱點內容
貓和老鼠哪個app好玩 瀏覽:716
網易有愛登錄器在哪個文件夾 瀏覽:244
iis6壓縮 瀏覽:140
redisphp擴展mac 瀏覽:199
狀態鏈路演算法 瀏覽:316
毛豆app裡面購車合同在哪裡 瀏覽:563
程序員上臂式電腦 瀏覽:56
php檢測中文 瀏覽:101
壓縮性骨折半年 瀏覽:561
如何雲伺服器解壓文件 瀏覽:932
單片機93加9E的psw 瀏覽:723
福建少兒頻道哪個app可以看 瀏覽:393
印度加密代幣機票 瀏覽:419
哪個app可以看江西2套 瀏覽:680
哪個小說app好用還免費 瀏覽:666
win7ping命令 瀏覽:508
程序員看圖識演算法 瀏覽:950
vs設置遠程編譯 瀏覽:600
速騰汽車怎麼給安卓手機充電 瀏覽:270
蘋果安卓換機用什麼軟體好 瀏覽:263