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

java導出zip包

發布時間:2021-03-05 12:09:15

⑴ 如何用java把內存里的二進制文件打包成ZIP包

Java代碼
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

class ZipTest {
// 壓縮
public static void zip(String zipFileName, String inputFile)
throws Exception {
File f = new File(inputFile);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, f, f.getName());
System.out.println("zip done");
out.close();
}

private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}

public static void main(String[] args) {
try {
ZipTest t = new ZipTest();
t.zip("c:\\test.zip", "c:\\1.txt");
} catch (Exception e) {
e.printStackTrace(System.out);
}

}
}

⑵ java導出zip壓縮文件a,a里含有b.xml文件和c.zip文件,c.zip文件里有多個xml文件,求指點!

⑶ 怎樣用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 html導出zip包 response如何設置

response.setContentType("application/zip");

⑸ java 如何將多個excl生成zip文件下載

/**
* 壓縮文件zip-Apache中ant所帶包
* @功能信息 :
* @參數信息 :
* @返回值信息 :
* @異常信息 :
* */
public void getZip(List list,String path,String fileName) throws Exception{
byte[] buffer = new byte[1024];

String strZipName = fileName + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path
+ strZipName));
for (int j = 0; j < list.size(); j++) {
String name = list.get(j).toString();
FileInputStream fis = new FileInputStream(path + name);
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
System.out.println("生成Demo.zip成功");
}

⑹ 如何用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來確定外部程序的執行情況。

⑺ 如何使用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包 報

相信大家在開發java的時候一定會遇到要求將java工程打包成可運行的jar的需求,今天我在這篇博客中詳細講解一下生成可運行jar的兩種方法,親測完全可行。
1. 工程中不包含第三方的jar包
這種情況比較簡單,選中需要生成jar的工程,右擊-->Export,

選中java--- > JAR file,
此處要:
選中 Export generated class files and resources;
選中 Compress the contents of the JAR file;
選中 Overwrite existing files without warning;
然後「Next」,

選中 Export class files with compile errors;
選中 Export class files with compile warnings;
點擊「Next」,

選擇「Main class」,then 「finish」 OK。
運行jar包的方法:
cmd,進入jar包所在的位置,運行命令 java -jar *.jar,就可以了。或者直接配置文件關聯。
2.工程包含第三方的jar包時
如果工程中包含第三方的jar包時,安裝上面的方法生產的jar包,在運行時會報java.lang.NoClassDefFoundError的錯,經過google, 很多網站之後,發現需要安裝eclipse的插件,插件地址是:http://sourceforge.net/projects/fjep/ 下載的文件是「net.sf.fjep.fatjar_0.0.31.zip」,解壓縮後你會看到plugins文件夾中的net.sf.fjep.fatjar_0.0.31.jar文件(簡稱Fat jar)。
Fat jar插件的安裝方式:
把Fat jar文件拷貝到你的eclipse目錄下的plugins中..重新啟動你的eclipse平台,然後查看Window---preferences,彈出的窗口中有Fat jar preferences這一項則說明你已經安裝成功了,沒有這一項則未安裝成功,需要多做如下幾步操作:到你的eclipse目錄下的configuration---org.eclipse.update,並刪除platform.xml文件,然後重新啟動eclipse.查看Window---preferences,你會發現Fat jar:

接下來就可以生成包含第三方jar包的可運行的jar包了。
選中工程,右擊 -- > Export --- > Other --- > Fat jar Exportor,

然後,next ---> 選中要生成jar包的工程 -- > next ,

在這里需要設置Main-class,它的值是工程運行的main class,並且勾上One-JAR,點擊 next;

即是項目引用的第三方類庫,點擊「finish」,這樣就生成了包含第三方jar包的可運行的jar包。以上上在基於Eclipse 3.6.2(藍色標題欄)和Eclipse 4.2.1(紅色標題欄)的效果,如果是Eclipse 3.6以下的版本(具體的版本不確定,因為我沒有試過),在選中工程右擊後,會有Build Fat Jar,

接下來的操作大同小異了。

⑼ java代碼實現 導出zip包,無法打開zip壓縮包

package com.lch.test;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZIP {
public static void main(String[] argv) throws Exception {
ZipFile zf = new ZipFile("E:\\wk\\LBSLEMIS201106141057\\LBSLEMIS\\test\\com\\lch\\test\\filename.zip");

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
String zipEntryName = ((ZipEntry) entries.nextElement()).getName();
System.out.println(zipEntryName);
}
}
}

用javad 的ZipFile類的ZipEntry方法試一下 找到ZIP裡面的ZipEntry方法 讀取Zip裡面壓縮文件的內容

有可能會引用外包

你好,我不知道你說的dzp是什麼格式文件,但如果是zip的壓縮文件,可以看下我的這段代碼

ZipFile file = new ZipFile("d:\\1.zip");
ZipEntry entry = file.getEntry("1.xml"); //假如壓縮包里的文件名是1.xml
InputStream in=file.getInputStream(entry);
最後就是按照java中一貫的流的處理方式即可

可以不解壓,zip包里的一個對象就是一個ZipEntry
找到你想要的那個ZipEntry,用文流寫出來就可以了。追問通過ZipEntry,然後用流就可以讀出裡面的內容了嗎?謝謝指點!
回答/**
* 解壓
* @param root 輸出目標
* @param zipfile zip文件
*/
protected void unzip(File root, File zipfile, String file) throws Exception {

// 解壓文件不存在時返回
if (!zipfile.exists()) {
return;
}
// 釋放目錄不存時創建
if (!root.exists()) {
root.mkdirs();
}
// 釋放目錄不為目錄時返回
if (!root.isDirectory()) {
return;
}

FileInputStream fin = new FileInputStream(zipfile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry entry = null;

while ((entry = zin.getNextEntry()) != null) {
// if (!entry.getName().endsWith(file)) {
// continue;
// }
File tmp = new File(root, entry.getName());
if (entry.isDirectory()) {
tmp.mkdirs();
} else {
byte[] buff = new byte[4096];
int len = 0;
tmp.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(tmp);
while ((len = zin.read(buff)) != -1) {
fout.write(buff, 0, len);
}
zin.closeEntry();
fout.close();
}
}

}
這里完整的解壓代碼。
// if (!entry.getName().endsWith(file)) {
// continue;
// }
這段打開就是只解出一個你指定的文件。

下面是測試用的。
public static void main(String[] args) throws Exception {
new CommonFiles().unzip(new File("D:\\"), new File("D:\\test.zip"),"file.txt");
}

這個例子會在D盤生成型個test文件夾,file.txt就會在裡面,(裡面也可能會有多個文件夾,這個取決於壓縮包里文件的度)

⑽ java導出為什麼彈出來的是.zip

把問題描述清楚點,你是想導出xlsx嗎? 你自己看下代碼,有個地方也許默認導出的是zip格式

閱讀全文

與java導出zip包相關的資料

熱點內容
生孩子電影 瀏覽:667
iphone中音樂怎麼刪除 瀏覽:542
關於愛情韓劇電影免費觀看 瀏覽:941
無保qq申訴上保 瀏覽:215
蘋果6splussd卡 瀏覽:802
庫樂隊app顯示19是什麼意思 瀏覽:701
言言直播的官方網站是多少 瀏覽:340
啄木鳥十大電影女同天堂 瀏覽:507
iris女主扮演者 瀏覽:144
51自學網cor視頻教程 瀏覽:243
怎麼用ps把臉p黑教程 瀏覽:223
播放iso藍光原盤app有哪些 瀏覽:115
尋呼機地址碼編程是什麼意思 瀏覽:947
數據包安全未知為什麼解壓不出來 瀏覽:69
愛人女主角 瀏覽:623
古龍小說下載 瀏覽:242
linux單詞統計 瀏覽:200
iphone音符符號 瀏覽:649
女設計師幾百年不死韓國 瀏覽:245
linux無法生成gbk文件 瀏覽:590

友情鏈接