Java編寫的模仿360壓縮工具--原創

第一步:建立壓縮格式和壓縮byte


package util;
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.Collection;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-8-2
 * @version 2.0
 * @since JDK1.6(建議)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:
 */
public class Compression {
    private static final int BUFF_SIZE = 1024 * 1024;     //1M Byte
    /**
     * 批量壓縮文件(夾)
     * @param resFileList 要壓縮的文件(夾)列表
     * @param zipFile         生成的壓縮文件
     * @throws IOException 當壓縮過程出錯時拋出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
            ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
            for (File resFile : resFileList) {
                    zipFile(resFile, zipout, "");
            }
            zipout.close();
    }
    /**
     * 批量壓縮文件(夾)
     * @param resFileList 要壓縮的文件(夾)列表
     * @param zipFile         生成的壓縮文件
     * @param comment         壓縮文件的註釋
     * @throws IOException 當壓縮過程出錯時拋出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
            ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
            for (File resFile : resFileList) {
                    zipFile(resFile, zipout, "");
            }
            zipout.setComment(comment);
            zipout.close();
    }
    /**
     * 解壓縮一個文件
     * @param zipFile        壓縮文件
     * @param folderPath 解壓縮的目標目錄
     * @throws IOException 當壓縮過程出錯時拋出
     */
    public static void upZipFile(File zipFile, String folderPath) throws IOException {
            ZipFile zf = new ZipFile(zipFile);
            for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
                    ZipEntry entry = ((ZipEntry) entries.nextElement());
                    InputStream in = zf.getInputStream(entry);
                    OutputStream out = new FileOutputStream(folderPath + File.separator + entry.getName());
                    byte buffer[] = new byte[BUFF_SIZE];
                    int realLength;
                    while ((realLength = in.read(buffer)) > 0) {
                            out.write(buffer, 0, realLength);
                    }
                    in.close();
                    out.close();
            }
    }
    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException {
            rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
            if (resFile.isDirectory()) {
                    File[] fileList = resFile.listFiles();
                    for (File file : fileList) {
                            zipFile(file, zipout, rootpath);
                    }
            } else {
                    byte buffer[] = new byte[BUFF_SIZE];
                    BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
                    zipout.putNextEntry(new ZipEntry(rootpath));
                    int realLength;
                    while ((realLength = in.read(buffer)) != -1) {
                            zipout.write(buffer, 0, realLength);
                    }
                    in.close();
                    zipout.flush();
                    zipout.closeEntry();
            }
    }
}

第二步:簡單測試


package test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import util.Compression;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-7-25
 * @version 2.0
 * @since JDK1.6(建議)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:
 */
public class CompressionTest {
    public static void main(String[] args) throws IOException {
            Collection<File> resFileList = new ArrayList<File>();
            resFileList.add(new File("C:\\這是一個測試文件.doc"));
            File zipFile = new File("C:\\txxxt.zip");
            Compression.zipFiles(resFileList, zipFile);
    }
}


讀者朋友在你的C盤建立一個------ 這是一個測試文件.doc------運行CompressionTest.java文件在你的C盤出現的這個txxxt.zip文件


但是打開我們的文件,卻出現亂碼了


這種問題很好解決:

使用下面的兩個包代替上面的兩個包

這個兩個包我下面會上傳進來的!

Java程序猿-160243674 歡迎大家的加入!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章