搜尋此網誌

2012年1月18日 星期三

(JAVA)要將檔案壓縮且加密的方式

首先下載下列兩個jar

接著寫一隻ZipUtil.java

如下

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;



import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

import org.springframework.stereotype.Component;



import com.sdunicomsi.ICT.edu.foundation.base.ObjectAncestor;



import de.idyl.crypto.zip.AesZipFileDecrypter;

import de.idyl.crypto.zip.AesZipFileEncrypter;

import de.idyl.crypto.zip.impl.ExtZipEntry;



@Component("zipUtil")

public class ZipUtil extends ObjectAncestor {



public Boolean compress(String inFile, String outFile, String key)throws Exception {



Boolean flag = false;

ArrayList<String> fileList = new ArrayList<String>();

fileList.add(inFile);

compress(fileList, outFile,key);

flag = true;



return flag;

}



public Boolean compress(List<String> inFiles, String outFile, String key)throws Exception {

Boolean flag = false;

String tempFile = outFile;

if(null!=key&&!"".equals(key)){

tempFile = tempFile+"temp";

}

ArrayList<String> filePathList = new ArrayList<String>();

ArrayList<String> fileNameList = new ArrayList<String>();

if(inFiles!=null&&inFiles.size()>0){

File file = null;

for(String fileStr:inFiles){

file = new File(fileStr);

filePathList.add(file.getAbsolutePath());

fileNameList.add(file.getName());

}

makeZip(filePathList, fileNameList, tempFile);



if(null!=key&&!"".equals(key)){

File temp = new File(tempFile);

AesZipFileEncrypter enc = new AesZipFileEncrypter(outFile);

enc.addAll(temp, key);

enc.close();

temp.delete();

}

}

flag = true;

return flag;

}



public Boolean compress(String inDir, String outFile, String key,

Boolean ifSubDir) throws Exception{

System.out.println("--inDir--"+inDir);

System.out.println("--outFile--"+outFile);

Boolean flag = false;



String tempFile = outFile;

if(null!=key&&!"".equals(key)){

tempFile = tempFile+"temp";

}

makeZip(new File(inDir),tempFile,ifSubDir);

if(null!=key&&!"".equals(key)){

File temp = new File(tempFile);

AesZipFileEncrypter enc = new AesZipFileEncrypter(outFile);

enc.addAll(temp, key);

enc.close();

temp.delete();

}

flag = true;

return flag;

}



public Boolean uncompress(String inFile, String outDir, String key,

Boolean ifSubDir) throws Exception{



boolean flag = false;



if(key==null||"".equals(key)){

unZip(inFile, new File(outDir));

}else{

AesZipFileDecrypter zipFile = new AesZipFileDecrypter( new File(inFile) );

for(ExtZipEntry entry : zipFile.getEntryList()){

if(!ifSubDir){

String filename = entry.getName();

String[] strArray = filename.split(File.separator+File.separator);

if(strArray!=null&&strArray.length==1){

zipFile.extractEntry(entry,new File(outDir+File.separator+entry.getName()), key);

}

}else{

zipFile.extractEntry(entry,new File(outDir+File.separator+entry.getName()), key);

}

}

}

flag = true;

return flag;

}





private void unZip(String zipFilePath, File toUnzipFold)

   throws IOException {

  if (!toUnzipFold.exists()) {

   toUnzipFold.mkdirs();

  }

  ZipFile zfile = new ZipFile(zipFilePath);

  // zList包含了所有的檔夾、目錄資訊

  Enumeration zList = zfile.getEntries();

  byte[] buf = new byte[1024];

  File tmpfile = null;

  File tmpfold = null;

  while (zList.hasMoreElements()) {

   ZipEntry ze = (ZipEntry) zList.nextElement();

   tmpfile = new File(toUnzipFold.getAbsolutePath() + File.separator

     + ze.getName());

   if (ze.isDirectory()) {

    continue;

   } else {

    tmpfold = tmpfile.getParentFile();

    if (!tmpfold.exists()) {

     tmpfold.mkdirs();

    }

    OutputStream os = new BufferedOutputStream(

      new FileOutputStream(tmpfile.getAbsolutePath()));

    InputStream is = new BufferedInputStream(zfile.getInputStream(ze));

    int readLen = 0;

    while ((readLen = is.read(buf, 0, 1024)) != -1) {

     os.write(buf, 0, readLen);

    }

    is.close();

    os.close();

   }

  }

}



private void makeZip(File fold, String outputZipFileName)

   throws IOException {

  makeZip(fold, outputZipFileName, false);

}



private void makeZip(File fold, String outputZipFileName,

   boolean recursive) throws IOException {

  if (fold.exists()) {

   ArrayList<File> fileList = new ArrayList<File>();

   listAllFile(fold, fileList, recursive);

   ArrayList<String> filePathList = new ArrayList<String>();

   ArrayList<String> fileNameList = new ArrayList<String>();

   String tmpStr = null;

   int basLen = fold.getAbsolutePath().length();

   for (File f : fileList) {

    tmpStr = f.getAbsolutePath();

    filePathList.add(tmpStr);

    fileNameList.add(tmpStr.substring(basLen+1, tmpStr.length()));

   }

  makeZip(filePathList, fileNameList, outputZipFileName);

  }

}



private void listAllFile(File fold, ArrayList<File> fileList,

   boolean recursive) {

  File[] files = fold.listFiles();

  for (File f : files) {

   if (recursive && f.isDirectory()) {

    listAllFile(f, fileList, recursive);

   } else if(!f.isDirectory()){

    fileList.add(f);

   }  

  }

}



private void makeZip(ArrayList<String> filePathList,

   ArrayList<String> fileNameList, String outputZipFileName)

   throws IOException {

  byte[] buf = new byte[1024];

  File outputZipFile = new File(outputZipFileName);

  File outputZipParentFile = outputZipFile.getParentFile();

  if(!outputZipParentFile.exists()){

  outputZipParentFile.mkdirs();

  }

  

  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));

  int len = -1;

  for (int i = 0; i < filePathList.size(); i++) {

   if (filePathList.get(i) != null) {

    FileInputStream in = new FileInputStream(filePathList.get(i));

    out.putNextEntry(new org.apache.tools.zip.ZipEntry(fileNameList

      .get(i)));

    while ((len = in.read(buf)) > 0) {

     out.write(buf, 0, len);

    }

    out.closeEntry();

    in.close();

   }

  }

  out.close();

}

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

ZipUtil z = new ZipUtil();

//壓縮

// z.compress("f:/xj_pt_student.csv", "f:/csv.zip", "123");

//  z.compress("f:/testk", "f:/testsub.zip", "",true);//無密碼,有子目錄

  z.compress("f:/testk", "f:/ttt/tttttt/tt/testsubk.zip", "123",true);//有密碼,有子目錄

//  z.compress("f:/testk", "f:/test.zip", "",false);//無密碼,無子目錄

// z.compress("f:/testk", "f:/testk.zip", "123",false);//無密碼,無子目錄

//解壓

// z.uncompress("f:/testsub.zip", "f:/testsub","",true);

// z.uncompress("f:/testsubk.zip", "f:/testsubk","123",true);

/
}


因為我只需要將一個檔案做壓縮加密,但是上述的解壓縮會有錯誤所以我將他刪除後改成下列的


package com.util;
//import h----.elearn.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import de.idyl.crypto.zip.AesZipFileEncrypter;
import java.util.zip.ZipOutputStream;


public class ZipUtil  {

    public Boolean compress(String inFile, String outFile, String key) throws Exception {
        Boolean flag = false;
        ArrayList<String> fileList = new ArrayList<String>();
        fileList.add(inFile);
        compress(fileList, outFile, key);
        flag = true;
        return flag;
    }

    public Boolean compress(List<String> inFiles, String outFile, String key) throws Exception {
        Boolean flag = false;
        String tempFile = outFile;
        if (null != key && !"".equals(key)) {
            tempFile = tempFile + "temp";
        }
        ArrayList<String> filePathList = new ArrayList<String>();
        ArrayList<String> fileNameList = new ArrayList<String>();
        if (inFiles != null && inFiles.size() > 0) {
            File file = null;
            for (String fileStr : inFiles) {
                file = new File(fileStr);
                filePathList.add(file.getAbsolutePath());
                fileNameList.add(file.getName());
            }
            makeZip(filePathList, fileNameList, tempFile);

            if (null != key && !"".equals(key)) {
                File temp = new File(tempFile);
                AesZipFileEncrypter enc = new AesZipFileEncrypter(outFile);
                enc.addAll(temp, key);
                enc.close();
                temp.delete();
            }
        }
        flag = true;
        return flag;
    }

    public Boolean compress(String inDir, String outFile, String key,
            Boolean ifSubDir) throws Exception {
        System.out.println("--inDir--" + inDir);
        System.out.println("--outFile--" + outFile);
        Boolean flag = false;

        String tempFile = outFile;
        if (null != key && !"".equals(key)) {
            tempFile = tempFile + "temp";
        }
        makeZip(new File(inDir), tempFile, ifSubDir);
        if (null != key && !"".equals(key)) {
            File temp = new File(tempFile);
            AesZipFileEncrypter enc = new AesZipFileEncrypter(outFile);
            enc.addAll(temp, key);
            enc.close();
            temp.delete();
        }
        flag = true;
        return flag;
    }


    private void makeZip(File fold, String outputZipFileName)
            throws IOException {
        makeZip(fold, outputZipFileName, false);
    }

    private void makeZip(File fold, String outputZipFileName,
            boolean recursive) throws IOException {
        if (fold.exists()) {
            ArrayList<File> fileList = new ArrayList<File>();
            listAllFile(fold, fileList, recursive);
            ArrayList<String> filePathList = new ArrayList<String>();
            ArrayList<String> fileNameList = new ArrayList<String>();
            String tmpStr = null;
            int basLen = fold.getAbsolutePath().length();
            for (File f : fileList) {
                tmpStr = f.getAbsolutePath();
                filePathList.add(tmpStr);
                fileNameList.add(tmpStr.substring(basLen + 1, tmpStr.length()));
            }
            makeZip(filePathList, fileNameList, outputZipFileName);
        }
    }

    private void listAllFile(File fold, ArrayList<File> fileList,
            boolean recursive) {
        File[] files = fold.listFiles();
        for (File f : files) {
            if (recursive && f.isDirectory()) {
                listAllFile(f, fileList, recursive);
            } else if (!f.isDirectory()) {
                fileList.add(f);
            }
        }
    }

    private void makeZip(ArrayList<String> filePathList,
            ArrayList<String> fileNameList, String outputZipFileName)
            throws IOException {
        byte[] buf = new byte[1024];
        File outputZipFile = new File(outputZipFileName);
        File outputZipParentFile = outputZipFile.getParentFile();
        if (!outputZipParentFile.exists()) {
            outputZipParentFile.mkdirs();
        }

        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));
        int len = -1;
        for (int i = 0; i < filePathList.size(); i++) {
            if (filePathList.get(i) != null) {
                FileInputStream in = new FileInputStream(filePathList.get(i));
                out.putNextEntry(new java.util.zip.ZipEntry(fileNameList.get(i)));
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
        }
        out.close();
    }

    public static void main(String args[]) throws Exception {
        ZipUtil z = new ZipUtil();
 z.compress("C:/test.inc", "c:/csv.zip", "123");
    }
}


參考網頁
http://blog.163.com/qiangyongbin2000@126/blog/static/77517819201131313112820/

沒有留言:

張貼留言