Search in sources :

Example 31 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project hudson-2.x by hudson.

the class ZipArchiver method visit.

public void visit(File f, String relativePath) throws IOException {
    if (f.isDirectory()) {
        ZipEntry dirZipEntry = new ZipEntry(relativePath + '/');
        // Setting this bit explicitly is needed by some unzipping applications (see HUDSON-3294).
        dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
        zip.putNextEntry(dirZipEntry);
        zip.closeEntry();
    } else {
        zip.putNextEntry(new ZipEntry(relativePath));
        FileInputStream in = new FileInputStream(f);
        int len;
        while ((len = in.read(buf)) > 0) zip.write(buf, 0, len);
        in.close();
        zip.closeEntry();
    }
    entriesWritten++;
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

Example 32 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project wechat by dllwh.

the class ZipUtil method zip.

/**
 * @Title: zip
 * @Description: 压缩文件或者文件目录到指定的zip或者rar包
 * @param zipFileName
 *            压缩产生的zip文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
 * @param source
 *            源路径,可以是文件,也可以目录
 * @param directory
 *            目标路径,压缩文件名
 */
public static void zip(String zipFileName, String source, String directory) {
    ZipOutputStream zos = null;
    BufferedInputStream bis = null;
    String fileName = StringUtils.isBlank(zipFileName) ? FilenameUtils.getName(source) : zipFileName;
    byte[] buffere = new byte[BUFFEREDSIZE];
    File temp = new File(source);
    try {
        if (StringUtils.isBlank(source)) {
            return;
        } else {
            if (StringUtils.isBlank(directory)) {
                // 检查对象是否是文件夹(即目录)
                if (temp.isDirectory()) {
                    directory = source + ".zip";
                } else {
                    if (source.indexOf(".") > 0) {
                        directory = source.substring(0, source.lastIndexOf(".")) + ".zip";
                    } else {
                        directory = source + ".zip";
                    }
                }
            } else {
                // 判断目标文件是否存在,若不存在则创建
                File dirFile = new File(directory);
                if (!dirFile.exists()) {
                    FileUtils.forceMkdir(dirFile);
                }
                if (fileName.indexOf(".") > 0) {
                    fileName = fileName.substring(0, fileName.indexOf("."));
                }
                directory += File.separator + fileName + ".zip";
            }
        }
        // 递归获得该文件下所有文件名(不包括目录名)
        List<File> fileList = FileUtilHelper.loadFileName(new File(source));
        zos = new ZipOutputStream(new FileOutputStream(directory), ConstantHelper.GBK);
        for (int i = 0; i < fileList.size(); i++) {
            File file = (File) fileList.get(i);
            zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
            bis = new BufferedInputStream(new FileInputStream(file));
            int length = 0;
            while ((length = bis.read(buffere)) != -1) {
                zos.write(buffere, 0, length);
            }
            bis.close();
            zos.closeEntry();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (zos != null) {
                zos.close();
            }
        } catch (Exception e) {
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ZipEntry (org.apache.tools.zip.ZipEntry)32 ZipFile (org.apache.tools.zip.ZipFile)17 File (java.io.File)13 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 FileInputStream (java.io.FileInputStream)7 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 Enumeration (java.util.Enumeration)5 ZipException (java.util.zip.ZipException)4 BuildException (org.apache.tools.ant.BuildException)4 ZipOutputStream (org.apache.tools.zip.ZipOutputStream)4 OutputStream (java.io.OutputStream)3 ZipResource (org.apache.tools.ant.types.resources.ZipResource)3 FileNotFoundException (java.io.FileNotFoundException)2 RandomAccessFile (java.io.RandomAccessFile)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 ZipOutputStream (java.util.zip.ZipOutputStream)2