導航:首頁 > 編程語言 > javazip指定文件

javazip指定文件

發布時間:2022-07-23 12:49:18

java 解壓ZIP中多層次文件到指定目錄

既然是所有的文件提取,那自然還是得目錄遍歷。
只不過,保存的時候,忽略目錄層次,那就不用再挨個的創建目錄了。
省去了你從子目錄返回時,還要記錄父目錄位置的麻煩。

Ⅱ java程序 壓縮指定文件 格式(zip)

直接右鍵單擊文件夾(所有的文件存放在一個文件夾裡面)然後選擇壓縮 前提是你要有 zip壓縮程序

Ⅲ java中怎麼解壓rar文件 到指定文件目錄中

1.代碼如下:
[java] view plain
<span style="font-size:18px;background-color: rgb(204, 204, 204);">package cn.gov.csrc.base.util;
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.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 將文件夾下面的文件
* 打包成zip壓縮文件
*
* @author admin
*
*/
public final class FileToZip {
private FileToZip(){}
/**
* 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
* @param sourceFilePath :待壓縮的文件路徑
* @param zipFilePath :壓縮後存放路徑
* @param fileName :壓縮後文件的名稱
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try {
File zipFile = new File(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 = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//關閉流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
public static void main(String[] args){
String sourceFilePath = "D:\\TestFile";
String zipFilePath = "D:\\tmp";
String fileName = "12700153file";
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}
}
</span>
2.結果如下:
文件打包成功!
3.到D:/tmp下查看,你會發現生成了一個zip壓縮包.

Ⅳ 如何使用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文件.

用java加密壓縮zip文件:
package com.ninemax.demo.zip.decrypt;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;

import org.apache.commons.io.FileUtils;

import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;

/**
* 壓縮指定文件或目錄為ZIP格式壓縮文件
* 支持中文(修改源碼後)
* 支持密碼(僅支持256bit的AES加密解密)
* 依賴bcprov項目(bcprov-jdk16-140.jar)
*
* @author zyh
*/
public class DecryptionZipUtil {

/**
* 使用指定密碼將給定文件或文件夾壓縮成指定的輸出ZIP文件
* @param srcFile 需要壓縮的文件或文件夾
* @param destPath 輸出路徑
* @param passwd 壓縮文件使用的密碼
*/
public static void zip(String srcFile,String destPath,String passwd) {
AESEncrypter encrypter = new AESEncrypterBC();
AesZipFileEncrypter zipFileEncrypter = null;
try {
zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);
/**
* 此方法是修改源碼後添加,用以支持中文文件名
*/
zipFileEncrypter.setEncoding("utf8");
File sFile = new File(srcFile);
/**
* AesZipFileEncrypter提供了重載的添加Entry的方法,其中:
* add(File f, String passwd)
* 方法是將文件直接添加進壓縮文件
*
* add(File f, String pathForEntry, String passwd)
* 方法是按指定路徑將文件添加進壓縮文件
* pathForEntry - to be used for addition of the file (path within zip file)
*/
doZip(sFile, zipFileEncrypter, "", passwd);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipFileEncrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 具體壓縮方法,將給定文件添加進壓縮文件中,並處理壓縮文件中的路徑
* @param file 給定磁碟文件(是文件直接添加,是目錄遞歸調用添加)
* @param encrypter AesZipFileEncrypter實例,用於輸出加密ZIP文件
* @param pathForEntry ZIP文件中的路徑
* @param passwd 壓縮密碼
* @throws IOException
*/
private static void doZip(File file, AesZipFileEncrypter encrypter,
String pathForEntry, String passwd) throws IOException {
if (file.isFile()) {
pathForEntry += file.getName();
encrypter.add(file, pathForEntry, passwd);
return;
}
pathForEntry += file.getName() + File.separator;
for(File subFile : file.listFiles()) {
doZip(subFile, encrypter, pathForEntry, passwd);
}
}

/**
* 使用給定密碼解壓指定壓縮文件到指定目錄
* @param inFile 指定Zip文件
* @param outDir 解壓目錄
* @param passwd 解壓密碼
*/
public static void unzip(String inFile, String outDir, String passwd) {
File outDirectory = new File(outDir);
if (!outDirectory.exists()) {
outDirectory.mkdir();
}
AESDecrypter decrypter = new AESDecrypterBC();
AesZipFileDecrypter zipDecrypter = null;
try {
zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);
AesZipFileDecrypter.charset = "utf-8";
/**
* 得到ZIP文件中所有Entry,但此處好像與JDK里不同,目錄不視為Entry
* 需要創建文件夾,entry.isDirectory()方法同樣不適用,不知道是不是自己使用錯誤
* 處理文件夾問題處理可能不太好
*/
List<ExtZipEntry> entryList = zipDecrypter.getEntryList();
for(ExtZipEntry entry : entryList) {
String eName = entry.getName();
String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);
File extractDir = new File(outDir, dir);
if (!extractDir.exists()) {
FileUtils.forceMkdir(extractDir);
}
/**
* 抽出文件
*/
File extractFile = new File(outDir + File.separator + eName);
zipDecrypter.extractEntry(entry, extractFile, passwd);
}
} catch (IOException e) {
e.printStackTrace();
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
try {
zipDecrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 測試
* @param args
*/
public static void main(String[] args) {
/**
* 壓縮測試
* 可以傳文件或者目錄
*/
// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");
// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");

unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");
}
}
壓縮多個文件時,有兩個方法(第一種沒試):
(1) 預先把多個文件壓縮成zip,然後調用enc.addAll(inZipFile, password);方法將多個zip文件加進來。
(2)針對需要壓縮的文件循環調用enc.add(inFile, password);,每次都用相同的密碼。

Ⅵ java編寫的代碼怎麼壓縮成zip文件

摘要 (1)可以壓縮文件,也可以壓縮文件夾

Ⅶ 怎樣用Java生成ZIP文件

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;


import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;


/**

* @project: Test

* @author chenssy

* @date 2013-7-28

* @Description: 文件壓縮工具類

* 將指定文件/文件夾壓縮成zip、rar壓縮文件

*/

public class CompressedFileUtil {

/**

* 默認構造函數

*/

public CompressedFileUtil(){

}


/**

* @desc 將源文件/文件夾生成指定格式的壓縮文件,格式zip

* @param resourePath 源文件/文件夾

* @param targetPath 目的壓縮文件保存路徑

* @return void

* @throws Exception

*/

public void compressedFile(String resourcesPath,String targetPath) throws Exception{

File resourcesFile = new File(resourcesPath); //源文件

File targetFile = new File(targetPath); //目的

//如果目的路徑不存在,則新建

if(!targetFile.exists()){

targetFile.mkdirs();

}

String targetName = resourcesFile.getName()+".zip"; //目的壓縮文件名

FileOutputStream outputStream = new FileOutputStream(targetPath+"\"+targetName);

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

createCompressedFile(out, resourcesFile, "");

out.close();

}

/**

* @desc 生成壓縮文件。

* 如果是文件夾,則使用遞歸,進行文件遍歷、壓縮

* 如果是文件,直接壓縮

* @param out 輸出流

* @param file 目標文件

* @return void

* @throws Exception

*/

public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{

//如果當前的是文件夾,則進行進一步處理

if(file.isDirectory()){

//得到文件列表信息

File[] files = file.listFiles();

//將文件夾添加到下一級打包目錄

out.putNextEntry(new ZipEntry(dir+"/"));

dir = dir.length() == 0 ? "" : dir +"/";

//循環將文件夾中的文件打包

for(int i = 0 ; i < files.length ; i++){

createCompressedFile(out, files[i], dir + files[i].getName()); //遞歸處理

}

}

else{ //當前的是文件,打包處理

//文件輸入流

FileInputStream fis = new FileInputStream(file);

out.putNextEntry(new ZipEntry(dir));

//進行寫操作

int j = 0;

byte[] buffer = new byte[1024];

while((j = fis.read(buffer)) > 0){

out.write(buffer,0,j);

}

//關閉輸入流

fis.close();

}

}

public static void main(String[] args){

CompressedFileUtil compressedFileUtil = new CompressedFileUtil();

try {

compressedFileUtil.compressedFile("G:\zip", "F:\zip");

System.out.println("壓縮文件已經生成...");

} catch (Exception e) {

System.out.println("壓縮文件生成失敗...");

e.printStackTrace();

}

}

}

運行程序結果如下:

如果是使用java.util下的java.util.zip進行打包處理,可能會出現中文亂碼問題,這是因為java的zip方法不支持編碼格式的更改,我們可以使用ant.java下的zip工具類來進行打包處理。所以需要將ant.jar導入項目的lib目錄下。

Ⅷ 如何用java讀取zip文件名和zip內文件的文件名,在線等

循環目錄下的文件,判斷附加名是zip的,然後用zipOutputstream和zipInputstream解壓縮到一個臨時目錄讀取zip內的文件名。這一步是無法跳過的,即使你在電腦上查看zip或者rar的某一個文件時也是全部解壓縮地,因為所有文件壓縮為一個文件後使用同一個壓縮演算法,只有全部解出來才能還原回來。

Ⅸ 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)
{
}
}

Ⅹ 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);
}
}
}

閱讀全文

與javazip指定文件相關的資料

熱點內容
歐洲cf玩什麼伺服器 瀏覽:527
如何連接另一台電腦上的共享文件夾 瀏覽:679
如何讓桌面文件夾搬家到e盤 瀏覽:71
java自動格式化 瀏覽:617
ipad怎麼查看文件夾大小 瀏覽:581
手工粘土解壓球 瀏覽:550
在線視頻教育源碼 瀏覽:39
快四十學什麼編程 瀏覽:754
gnumakelinux 瀏覽:537
視易峰雲伺服器怎麼改系統 瀏覽:535
javamap取值 瀏覽:768
mac和win磁碟加密軟體 瀏覽:474
蘋果為什麼會連接不到伺服器 瀏覽:726
pdf格式文件如何保存 瀏覽:303
小霸王伺服器tx什麼意思 瀏覽:75
解釋dns命令 瀏覽:584
dmx512怎麼編程 瀏覽:744
北京雲主機17t雲伺服器 瀏覽:232
php伺服器url地址 瀏覽:440
哪裡看書免費app 瀏覽:437