導航:首頁 > 編程語言 > java打包成zip

java打包成zip

發布時間:2022-07-25 07:34:36

1. 如何用java調用本地命令生成一個zip包

首先你需要了解zip命令格式,然後在Java程序中調用本地命令使用:
Process proc = Runtime.getRuntime.exec("zip ..");
int result = proc.waitFor(); //等待本地進程的結束
if(result == 0){
System.out.println("打包成功");
}else{
...}
Java中默認認為返回的結果為0表示正常結束,非0則有問題;但這跟具體的操作系統和外部程序有關,有時候程序正常結束也可能返回1,這點需要注意~~,因此可以不根據result來確定外部程序的執行情況。

2. 如何用JAVA把內存里的二進制文件打包成ZIP包

在JDK中有一個zip工具類:<blockquote>java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.</blockquote>使用此類可以將文件夾或者多個文件進行打包壓縮操作。在使用之前先了解關鍵方法:<blockquote>ZipEntry(String name) Creates a new zip entry with the specified name.</blockquote>使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:<pre t="code" l="java">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 = "file";
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}

}

3. java打zip包

你可以通過Java內置的打包功能完成啊,單擊右鍵可以選擇Export裡面有很多的打包類型可以供你選擇

4. 求:關於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;

5. 怎樣用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目錄下。

6. java 如何將多個文件打包成一個zip

java 將多個文件打包成一個zip的代碼如下:
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
for(int i=0;i<fileList.size();i++){
String filename = (String)fileList.get(i);
File file = new File(filename);
zip(out,file);
}
out.close();

7. 如何使用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 版本問題*/

8. java怎麼將文件打包成zip包,並且源文件還在,只是多了個zip包,網上多是直接壓縮的,導致源文件不在了

選擇這個添加到壓縮文件,就是保留之前的,多出一份壓縮的啊

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

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

10. java 如何將多個文件打包成一個zip後進行下載

打包壓縮的如下:
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
for(int i=0;i<fileList.size();i++){
String filename = (String)fileList.get(i);
File file = new File(filename);
zip(out,file);
}
out.close();

下載的如下:
private int blockSize=65000;
File file = new File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
int readBytes = 0;
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
out.write(b, 0, readBytes);

代碼大致如此,請參考。

閱讀全文

與java打包成zip相關的資料

熱點內容
dota2怎麼設置國服伺服器地址 瀏覽:212
單片機高電平驅動 瀏覽:115
ios多選文件夾 瀏覽:909
加強行車調度命令管理 瀏覽:243
伺服器已禁用什麼意思 瀏覽:150
部隊命令回復 瀏覽:755
神奇寶貝伺服器地圖怎麼設置 瀏覽:382
加密演算法輸出固定長度 瀏覽:862
程序員去重慶還是武漢 瀏覽:121
伺服器如何撤銷網頁登錄限制 瀏覽:980
微信公眾平台php開發視頻教程 瀏覽:628
怎麼看蘋果授權綁定的app 瀏覽:255
壓縮機單級壓縮比 瀏覽:380
linux測試php 瀏覽:971
什麼時候梁旁邊需要加密箍筋 瀏覽:40
微信清粉軟體源碼 瀏覽:717
matlabdoc命令 瀏覽:550
如何去ping伺服器 瀏覽:75
ecshop安裝php55 瀏覽:817
javaword庫 瀏覽:958