精华内容
下载资源
问答
  • 它可以通过包java.util.Zip轻松解决,不需要任何额外的Jar文件只需复制以下代码并使用IDE运行它//Import all needed packagespackage general;import java.io.File;import java.io.FileInputStream;import java.io....

    它可以通过包java.util.Zip轻松解决,不需要任何额外的Jar文件

    只需复制以下代码并使用IDE运行它

    //Import all needed packages

    package general;

    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 java.util.zip.ZipEntry;

    import java.util.zip.ZipOutputStream;

    public class ZipUtils {

    private List fileList;

    private static final String OUTPUT_ZIP_FILE = "Folder.zip";

    private static final String SOURCE_FOLDER = "D:\\Reports"; // SourceFolder path

    public ZipUtils() {

    fileList = new ArrayList < String > ();

    }

    public static void main(String[] args) {

    ZipUtils appZip = new ZipUtils();

    appZip.generateFileList(new File(SOURCE_FOLDER));

    appZip.zipIt(OUTPUT_ZIP_FILE);

    }

    public void zipIt(String zipFile) {

    byte[] buffer = new byte[1024];

    String source = new File(SOURCE_FOLDER).getName();

    FileOutputStream fos = null;

    ZipOutputStream zos = null;

    try {

    fos = new FileOutputStream(zipFile);

    zos = new ZipOutputStream(fos);

    System.out.println("Output to Zip : " + zipFile);

    FileInputStream in = null;

    for (String file: this.fileList) {

    System.out.println("File Added : " + file);

    ZipEntry ze = new ZipEntry(source + File.separator + file);

    zos.putNextEntry(ze);

    try {

    in = new FileInputStream(SOURCE_FOLDER + File.separator + file);

    int len;

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

    zos.write(buffer, 0, len);

    }

    } finally {

    in.close();

    }

    }

    zos.closeEntry();

    System.out.println("Folder successfully compressed");

    } catch (IOException ex) {

    ex.printStackTrace();

    } finally {

    try {

    zos.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    public void generateFileList(File node) {

    // add file only

    if (node.isFile()) {

    fileList.add(generateZipEntry(node.toString()));

    }

    if (node.isDirectory()) {

    String[] subNote = node.list();

    for (String filename: subNote) {

    generateFileList(new File(node, filename));

    }

    }

    }

    private String generateZipEntry(String file) {

    return file.substring(SOURCE_FOLDER.length() + 1, file.length());

    }

    }

    参考mkyong ..我改变了当前问题要求的代码

    展开全文
  • importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.i...

    packageutils;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.nio.file.FileVisitResult;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.nio.file.SimpleFileVisitor;importjava.nio.file.attribute.BasicFileAttributes;importjava.util.Objects;importorg.apache.commons.compress.archivers.ArchiveEntry;importorg.apache.commons.compress.archivers.ArchiveInputStream;importorg.apache.commons.compress.archivers.ArchiveOutputStream;importorg.apache.commons.compress.archivers.zip.ZipArchiveEntry;importorg.apache.commons.compress.archivers.zip.ZipArchiveInputStream;importorg.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;importorg.apache.commons.compress.utils.IOUtils;/*** 压缩文件夹工具类

    **/

    public classCompressUtils {/*** 压缩文件夹到指定zip文件

    *

    *@paramsrcDir 源文件夹

    *@paramtargetFile 目标知道zip文件

    *@throwsIOException IO异常,抛出给调用者处理*/

    public static void zip(String srcDir, String targetFile) throwsIOException {try(

    OutputStream outputStream= newFileOutputStream(targetFile);

    ) {

    zip(srcDir, outputStream);

    }

    }/*** 压缩文件夹到指定输出流中,可以是本地文件输出流,也可以是web响应下载流

    *

    *@paramsrcDir 源文件夹

    *@paramoutputStream 压缩后文件的输出流

    *@throwsIOException IO异常,抛出给调用者处理*/

    public static void zip(String srcDir, OutputStream outputStream) throwsIOException {try(

    BufferedOutputStream bufferedOutputStream= newBufferedOutputStream(outputStream);

    ArchiveOutputStream out= newZipArchiveOutputStream(bufferedOutputStream);

    ) {

    Path start=Paths.get(srcDir);

    Files.walkFileTree(start,new SimpleFileVisitor() {

    @Overridepublic FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throwsIOException {

    ArchiveEntry entry= newZipArchiveEntry(dir.toFile(), start.relativize(dir).toString());

    out.putArchiveEntry(entry);

    out.closeArchiveEntry();return super.preVisitDirectory(dir, attrs);

    }

    @Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throwsIOException {try(

    InputStream input= newFileInputStream(file.toFile())

    ) {

    ArchiveEntry entry= newZipArchiveEntry(file.toFile(), start.relativize(file).toString());

    out.putArchiveEntry(entry);

    IOUtils.copy(input, out);

    out.closeArchiveEntry();

    }return super.visitFile(file, attrs);

    }

    });

    }

    }/*** 解压zip文件到指定文件夹

    *

    *@paramzipFileName 源zip文件路径

    *@paramdestDir 解压后输出路径

    *@throwsIOException IO异常,抛出给调用者处理*/

    public static void unzip(String zipFileName, String destDir) throwsIOException {try(

    InputStream inputStream= newFileInputStream(zipFileName);

    ) {

    unzip(inputStream, destDir);

    }

    }/*** 从输入流中获取zip文件,并解压到指定文件夹

    *

    *@paraminputStream zip文件输入流,可以是本地文件输入流,也可以是web请求上传流

    *@paramdestDir 解压后输出路径

    *@throwsIOException IO异常,抛出给调用者处理*/

    public static void unzip(InputStream inputStream, String destDir) throwsIOException {try(

    BufferedInputStream bufferedInputStream= newBufferedInputStream(inputStream);

    ArchiveInputStream in= newZipArchiveInputStream(bufferedInputStream);

    ) {

    ArchiveEntry entry= null;while (Objects.nonNull(entry =in.getNextEntry())) {if(in.canReadEntryData(entry)) {

    File file=Paths.get(destDir, entry.getName()).toFile();if(entry.isDirectory()) {if (!file.exists()) {

    file.mkdirs();

    }

    }else{try(

    OutputStream out= newFileOutputStream(file);

    ) {

    IOUtils.copy(in, out);

    }

    }

    }else{

    System.out.println(entry.getName());

    }

    }

    }

    }

    }

    展开全文
  • 前几天写了文章介绍java文件夹压缩(见http://www.voidcn.com/article/p-naroxxqs-bqy.html),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能...

    前几天写了文章介绍java文件夹的压缩(见

    http://www.voidcn.com/article/p-naroxxqs-bqy.html),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能支持linux压缩,有两点:

    1:再压缩前,要设置linux模式,

    如果是文件目录,则

    ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));

    zipEntry.setUnixMode(755);//解决linux乱码

    如果是文件,则

    ZipEntry zipEntry=new ZipEntry(base);

    zipEntry.setUnixMode(644);//解决linux乱码

    然后在输出时强制设置编码:

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(

    zipFileName));

    out.setEncoding("GBK");//解决linux乱码

    全部代码如下:

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.OutputStream;

    import org.apache.tools.zip.ZipEntry;

    import org.apache.tools.zip.ZipOutputStream;

    /**

    * 文件夹压缩,支持win和linux

    * @author wlzhang

    *

    */

    public class ZipUtil

    {

    /**

    * @param inputFileName

    * 输入一个文件夹

    * @param zipFileName

    * 输出一个压缩文件夹,打包后文件名字

    * @throws Exception

    */

    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception

    {

    return zip(zipFileName, new File(inputFileName));

    }

    private static OutputStream zip(String zipFileName, File inputFile) throws Exception

    {

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(

    zipFileName));

    out.setEncoding("GBK");//解决linux乱码

    zip(out, inputFile, "");

    out.close();

    return out;

    }

    private static void zip(ZipOutputStream out, File f, String base) throws Exception

    {

    if (f.isDirectory())

    { // 判断是否为目录

    File[] fl = f.listFiles();

    // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));

    // out.putNextEntry(new ZipEntry(base + "/"));

    ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));

    zipEntry.setUnixMode(755);//解决linux乱码

    out.putNextEntry(zipEntry);

    // base = base.length() == 0 ? "" : base + "/";

    base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");

    for (int i = 0; i < fl.length; i++)

    {

    zip(out, fl[i], base + fl[i].getName());

    }

    }

    else

    { // 压缩目录中的所有文件

    // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));

    ZipEntry zipEntry=new ZipEntry(base);

    zipEntry.setUnixMode(644);//解决linux乱码

    out.putNextEntry(zipEntry);

    FileInputStream in = new FileInputStream(f);

    int b;

    while ((b = in.read()) != -1)

    {

    out.write(b);

    }

    in.close();

    }

    }

    } 以上代码就同时支持win和linux

    展开全文
  • package ...import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import...

    package com.app.common.util;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.util.Enumeration;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    import org.apache.tools.zip.ZipEntry;

    import org.apache.tools.zip.ZipFile;

    import org.apache.tools.zip.ZipOutputStream;

    public class ZipUtil {

    private static ZipUtil instance = new ZipUtil();

    private static final int BUFFEREDSIZE = 1024;

    private static Log log = LogFactory.getLog(ZipUtil.class);

    private ZipUtil() {

    }

    public static ZipUtil getInstance() {

    return instance;

    }

    public static void zip(String inputFilename, String zipFilename)

    throws IOException {

    zip(new File(inputFilename), zipFilename);

    }

    public static void zip(File inputFile, String zipFilename)

    throws IOException {

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(

    zipFilename));

    out.setEncoding("GBK");

    try {

    if (log.isDebugEnabled()) {

    log.debug("压缩开始:待压缩文件(夹)为 " + inputFile);

    }

    zip(inputFile, out, "");

    } catch (IOException e) {

    log.error("压缩失败");

    } finally {

    out.close();

    if (log.isDebugEnabled()) {

    log.debug("压缩成功");

    }

    }

    }

    private static void zip(File inputFile, ZipOutputStream out, String base)

    throws IOException {

    if (inputFile.isDirectory()) {

    if (inputFile.listFiles().length == 0) {

    ZipEntry zipEntry = new ZipEntry(base + inputFile.getName()

    + "/");

    out.putNextEntry(zipEntry);

    out.closeEntry();

    } else {

    base += inputFile.getName() + inputFile.separator;

    for (File f : inputFile.listFiles()) {

    zip(f, out, base);

    }

    }

    } else {

    ZipEntry zipEntry = new ZipEntry(base + inputFile.getName());

    out.putNextEntry(zipEntry);

    FileInputStream in = new FileInputStream(inputFile);

    try {

    int c;

    byte[] by = new byte[BUFFEREDSIZE];

    while ((c = in.read(by)) != -1) {

    out.write(by, 0, c);

    }

    } catch (IOException e) {

    throw e;

    } finally {

    in.close();

    }

    }

    }

    public static void multiZip(String[] inputFilelist, String zipFilename)

    throws IOException {

    File[] files = new File[inputFilelist.length];

    for (int i = 0; i < inputFilelist.length; i++) {

    files[i] = new File(inputFilelist[i]);

    }

    multiZip(files, zipFilename);

    }

    public static void multiZip(File[] files, String zipFilename)

    throws IOException {

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(

    zipFilename));

    out.setEncoding("GBK");

    try {

    if (log.isDebugEnabled()) {

    log.debug("压缩开始:待压缩文件(夹)共 " + files.length + "个");

    }

    for (File f : files) {

    zip(f, out, "");

    }

    } catch (Exception e) {

    log.error("压缩失败");

    } finally {

    out.close();

    if (log.isDebugEnabled()) {

    log.debug("压缩成功");

    }

    }

    }

    }

    展开全文
  • 展开全部import java.io.File;.../*** 压缩文件构32313133353236313431303231363533e4b893e5b19e31333365633962造函数* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip*/public ...
  • importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjavax.swing.JFileChooser;importjavax.swing.UIManager;importor...package com.yinyong.zip;imp...
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼一堆嵌套的文件夹 只有文件夹没有文件 我用了下面代码压缩出来的总是一个0KB的损坏的文件 问题出在哪里? 在线等感觉好像一直在递归文件夹 没有压缩的环节。。。private ...
  • java.util.zip.ZipOutputStream org.apache.tools.ant 注意 最初采用java.util.zip.ZipOutputStream的方式, jdk为1.8,出现BUG: 一定情况下,会将空文件夹变成文件格式,如: 空文件:鉴定人资格 鉴定人资格证书 ...
  • Java对Zip文件的支持不是很强大,有一些需要自己实现的代码,我在网上找了很多代码,都不能用于生产,要不就是流没有被关闭,要不就是Exception处理很随意,下面是我修改过并测试过的代码能用于生产的压缩和解压缩的...
  • 直接上代码: ... import org.slf4j.Logger;...import java.io.*; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * @ClassName TestCreateZip * @Description D
  • 前几天写了文章介绍java文件夹压缩(见http://www.voidcn.com/article/p-naroxxqs-bqy.html),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能...
  • 本文实例为大家分享了java实现文件夹解压和压缩的具体代码,供大家参考,具体内容如下效果实现多个文件以及文件夹压缩和解压代码分析import java.io.File;import java.io.FileInputStream;import java.io....
  • import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java....
  • 下面为我自己写的一个实例,代码如下:package ...import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;...
  • 将代码过程较好的代码段备份一下,下边资料是关于java压缩指定目录下的所有文件和文件夹的代码,希望对码农有帮助。String sourceDir="E:\test";int parentDirectoryLen=sourceDir.lastIndexOf(File.separator)+1;...
  • 使用java压缩一个文件夹中的任意几个文件之前有人写过用递归压缩一个文件夹里的所有文件,看了让人受益匪浅,我这个程序没有那么复杂,但是在我现在的实际的项目中,根本不会用到压缩整个文件夹,需要选择文件来压缩...
  • import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import ...
  • 23 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.OutputStream;8 importjava.util.ArrayList;9 imp...
  • import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.ut...
  • 展开全部在JDK中有一个zip工具类:java.util.zip Provides classes for reading ...使用此类可以将文件夹或者多个文件进行打包压缩操作。在使用之前先了解62616964757a686964616fe59b9ee7ad9431333363376462关键方法...
  • java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用java API进行zip递归压缩文件夹以及解压 ),也可以使用GZIPOutputStream去创建gzip(gz)压缩文件,但是java中没有一种官方的...
  • 压缩当前文件夹下所有文件,压缩为a.zip。命令行的方法是怎样。zip -r fileName.zip 文件夹名tartar命令可以用来压缩打包单文件、多个文件、单个目录、多个目录。常用格式:单个文件压缩打包 tar czvf my.tar file1...
  • import java.io.*;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/*** 文件操作* Created by heavenick on 2015/7/8.*/public class FileUtil {public static void ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 101,005
精华内容 40,402
关键字:

java压缩文件夹

java 订阅