Java—将文件夹压缩为zip文件,java文件夹压缩zip

manbetx手机网页版,Python压缩钦定的文书及文件夹为.zip

Java—将文件夹压缩为zip文件,java文件夹压缩zip

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
 * 
 * @author hwt
 *
 */
public class TestDir {
    /**
     * 将文件夹以及其下的文件压缩为文件
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //源文件夹
        File file = new File("D:/ziptest");
     //目的文件
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:/test.zip"));
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i=0; i<files.length; i++) {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
                zos.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
                while (true) {
                    byte[] b = new byte[100];
                    int len = bis.read(b);
                    if (len == -1)
                        break;
                    zos.write(b, 0, len);
                }
                bis.close();
            }
        }
        zos.close();
    }
}

 

http://www.bkjia.com/Javabc/1284107.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1284107.htmlTechArticleJava—将文件夹压缩为zip文件,java文件夹压缩zip
import java.io.BufferedInputStream; import java.io.File; import
java.io.FileInputStream; import java.io.FileOutputStr…

 

代码:

def zipDir(dirpath,outFullName):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 压缩文件保存路径+xxxx.zip
    :return: 无
    """
    zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
    for path,dirnames,filenames in os.walk(dirpath):
        # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
        fpath = path.replace(dirpath,'')

        for filename in filenames:
            zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
    zip.close()

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图