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

javazip寫文件

發布時間:2022-11-03 22:09:53

Ⅰ 急!!java linux 從伺服器上創建一個zip包 向zip包里寫文件 下載zip包中文亂碼

用的是什麼zip庫,有沒有設置編碼格式。
例如,zip4j的話,zf.setFileNameCharset("UTF-8");

Ⅱ 怎樣用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(){

Ⅲ JAVA 將文件寫入壓縮文件中

這還不簡單啊,不是有壓縮流,加壓放進去,

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

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

Ⅳ 如何使用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壓縮包。
首先需要由需要壓縮的文件創建一個InputStream對象,然後讀取文件內容寫入到ZipOutputStream中。
ZipOutputStream類接受FileOutputStream作為參數。創建號ZipOutputStream對象後需要創建一個zip entry,然後寫入。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
*
* @author outofmemory.cn
*/
public class Main {

/**
* Creates a zip file
*/

public void createZipFile() {

try {
String inputFileName = "test.txt";
String zipFileName = "compressed.zip";

//Create input and output streams
FileInputStream inStream = new FileInputStream(inputFileName);
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));

// Add a zip entry to the output stream
outStream.putNextEntry(new ZipEntry(inputFileName));

byte[] buffer = new byte[1024];
int bytesRead;

//Each chunk of data read from the input stream
//is written to the output stream
while ((bytesRead = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, bytesRead);
}

//Close zip entry and file streams
outStream.closeEntry();

outStream.close();
inStream.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().createZipFile();
}

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

寫了一個 樓主試試吧
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生成ZIP文件

在你聲明ZipEntry的時候在name後加上.xml後綴就可以了!!!
實例如下:

public static void main(String[] arg) throws Exception{

String xml;

/*
* 生成你的xml數據,存在String xml中。
*/

ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("D://test.zip"));
//聲明ZipOutputStream,用來輸出zip文件。

ZipEntry entry = new ZipEntry("test.xml");
//聲明ZipEntry

zipOut.putNextEntry(entry);
//將entry加入到zipOut中。

DataOutputStream dataOs = new DataOutputStream(zipOut);
//利用DataOutputStream對ZipOutputStream進行包裝。
dataOs.writeUTF(gd);
//輸出zip文件。
dataOs.close();
}

運行後,在D盤里就有一個test.zip文件,里包含的就是一個test.xml文件了。

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

閱讀全文

與javazip寫文件相關的資料

熱點內容
冰箱壓縮機燙手老跳閘 瀏覽:254
php日誌系統架構 瀏覽:453
udp獲取伺服器ip地址 瀏覽:985
能把心裡的恐懼解壓出來的視頻 瀏覽:368
三豐雲上傳伺服器流程 瀏覽:812
php類常亮 瀏覽:819
如何用紙尿褲做解壓玩具 瀏覽:608
程序員年齡和工資 瀏覽:766
壓縮空氣的特性簡介 瀏覽:564
廣樂美app是做什麼的 瀏覽:323
android的spinner屬性 瀏覽:929
店家幫平台源碼 瀏覽:973
源碼編輯器繪制圖形 瀏覽:951
長沙雲伺服器提供商 瀏覽:107
51單片機測脈沖寬度 瀏覽:286
文件夾弄成二維碼 瀏覽:283
python字典循環添加 瀏覽:692
閑置伺服器怎麼收費 瀏覽:162
閱讀app是用什麼開發的 瀏覽:37
js賦值給java 瀏覽:41