中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java將文件或是文件夾打包壓縮成zip格式

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *將文件或是文件夾打包壓縮成zip格式
 * @author ysc
 */
public class ZipUtils {
    private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);

    private ZipUtils(){};

    /**
     * APDPlat中的重要打包機(jī)制
     * 將jar文件中的某個(gè)文件夾里面的內(nèi)容復(fù)制到某個(gè)文件夾
     * @param jar 包含靜態(tài)資源的jar包
     * @param subDir jar中包含待復(fù)制靜態(tài)資源的文件夾名稱
     * @param loc 靜態(tài)資源復(fù)制到的目標(biāo)文件夾
     * @param force 目標(biāo)靜態(tài)資源存在的時(shí)候是否強(qiáng)制覆蓋
     */
    public static void unZip(String jar, String subDir, String loc, boolean force){
        try {
            File base=new File(loc);
            if(!base.exists()){
                base.mkdirs();
            }

            ZipFile zip=new ZipFile(new File(jar));
            Enumeration<? extends ZipEntry> entrys = zip.entries();
            while(entrys.hasMoreElements()){
                ZipEntry entry = entrys.nextElement();
                String name=entry.getName();
                if(!name.startsWith(subDir)){
                    continue;
                }
                //去掉subDir
                name=name.replace(subDir,"").trim();
                if(name.length()<2){
                    log.debug(name+" 長(zhǎng)度 < 2");
                    continue;
                }
                if(entry.isDirectory()){
                    File dir=new File(base,name);
                    if(!dir.exists()){
                        dir.mkdirs();
                        log.debug("創(chuàng)建目錄");
                    }else{
                        log.debug("目錄已經(jīng)存在");
                    }
                    log.debug(name+" 是目錄");
                }else{
                    File file=new File(base,name);
                    if(file.exists() && force){
                        file.delete();
                    }
                    if(!file.exists()){
                        InputStream in=zip.getInputStream(entry);
                        FileUtils.copyFile(in,file);
                        log.debug("創(chuàng)建文件");
                    }else{
                        log.debug("文件已經(jīng)存在");
                    }
                    log.debug(name+" 不是目錄");
                }
            }
        } catch (ZipException ex) {
            log.error("文件解壓失敗",ex);
        } catch (IOException ex) {
            log.error("文件操作失敗",ex);
        }
    }

   /**
     * 創(chuàng)建ZIP文件
     * @param sourcePath 文件或文件夾路徑
     * @param zipPath 生成的zip文件存在路徑(包括文件名)
     */
    public static void createZip(String sourcePath, String zipPath) {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipPath);
            zos = new ZipOutputStream(fos);
            writeZip(new File(sourcePath), "", zos);
        } catch (FileNotFoundException e) {
            log.error("創(chuàng)建ZIP文件失敗",e);
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (IOException e) {
                log.error("創(chuàng)建ZIP文件失敗",e);
            }

        }
    }

    private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
        if(file.exists()){
            if(file.isDirectory()){//處理文件夾
                parentPath+=file.getName()+File.separator;
                File [] files=file.listFiles();
                for(File f:files){
                    writeZip(f, parentPath, zos);
                }
            }else{
                FileInputStream fis=null;
                try {
                    fis=new FileInputStream(file);
                    ZipEntry ze = new ZipEntry(parentPath + file.getName());
                    zos.putNextEntry(ze);
                    byte [] content=new byte[1024];
                    int len;
                    while((len=fis.read(content))!=-1){
                        zos.write(content,0,len);
                        zos.flush();
                    }

                } catch (FileNotFoundException e) {
                    log.error("創(chuàng)建ZIP文件失敗",e);
                } catch (IOException e) {
                    log.error("創(chuàng)建ZIP文件失敗",e);
                }finally{
                    try {
                        if(fis!=null){
                            fis.close();
                        }
                    }catch(IOException e){
                        log.error("創(chuàng)建ZIP文件失敗",e);
                    }
                }
            }
        }
    }    
    public static void main(String[] args) {
        ZipUtils.createZip("D:\\workspaces\\netbeans\\APDPlat\\APDPlat_Web\\target\\APDPlat_Web-2.2\\platform\\temp\\backup", "D:\\workspaces\\netbeans\\APDPlat\\APDPlat_Web\\target\\APDPlat_Web-2.2\\platform\\temp\\backup.zip");
        ZipUtils.createZip("D:\\workspaces\\netbeans\\APDPlat\\APDPlat_Web\\target\\APDPlat_Web-2.2\\platform\\index.jsp", "D:\\workspaces\\netbeans\\APDPlat\\APDPlat_Web\\target\\APDPlat_Web-2.2\\platform\\index.zip");

    }
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Java獲得代理環(huán)境下的真實(shí)IP

下一篇:用C語(yǔ)言寫的一個(gè)萬(wàn)年歷