Java Code Examples for org.apache.tools.zip.ZipOutputStream#putNextEntry()

The following examples show how to use org.apache.tools.zip.ZipOutputStream#putNextEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Files.java    From development with Apache License 2.0 6 votes vote down vote up
public void writeTo(OutputStream out) throws IOException {
    final Set<String> filenames = new HashSet<String>();
    final ZipOutputStream zipout = new ZipOutputStream(out);
    for (IFile f : container.getFiles()) {
        assertNoAbsolutePath(f);
        assertNoDuplicates(filenames, f);
        ZipEntry entry = new ZipEntry(f.getLocalPath());
        entry.setTime(f.getLastModified());
        if (f.getPermissions() != IFile.UNDEF_PERMISSIONS) {
            entry.setUnixMode(f.getPermissions());
        }
        zipout.putNextEntry(entry);
        f.writeTo(zipout);
        zipout.closeEntry();
    }
    zipout.finish();
}
 
Example 2
Source File: ZipUtil.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
public static void zip(String path, List<File> files) throws IOException {
	ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(   
			new FileOutputStream(path), 1024));   
	for(File f : files) {
		String zipName = f.getName();
		if(!f.getName().contains(".png")) { 
			zipName = f.getName() + ".xml"; 
		}
		ZipEntry ze = new ZipEntry(zipName);
		ze.setTime(f.lastModified());
	    DataInputStream dis = new DataInputStream(new BufferedInputStream(   
	                new FileInputStream(f)));   
        zos.putNextEntry(ze);   
        int c;   
        while ((c = dis.read()) != -1) {   
            zos.write(c);   
        }   
	}
	zos.setEncoding("gbk");    
       zos.closeEntry();   
       zos.close();  
}
 
Example 3
Source File: ZipUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 功能:执行文件压缩成zip文件
 * 
 * @param source
 * @param basePath
 *            待压缩文件根目录
 * @param zos
 */

private static void zipFile(File source, String basePath, ZipOutputStream zos) {
	File[] files = new File[0];
	if (source.isDirectory()) {
		files = source.listFiles();
	} else {
		files = new File[1];
		files[0] = source;
	}
	String pathName;// 存相对路径(相对于待压缩的根目录)
	byte[] buf = new byte[1024];
	int length = 0;
	try {
		for (File file : files) {
			if (file.isDirectory()) {
				pathName = file.getPath().substring(basePath.length() + 1) + "/";
				zos.putNextEntry(new ZipEntry(pathName));
				zipFile(file, basePath, zos);
			} else {
				pathName = file.getPath().substring(basePath.length() + 1);
				InputStream is = new FileInputStream(file);
				BufferedInputStream bis = new BufferedInputStream(is);
				zos.putNextEntry(new ZipEntry(pathName));
				while ((length = bis.read(buf)) > 0) {
					zos.write(buf, 0, length);
				}
				is.close();
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: FileUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 将目录压缩到ZIP输出流
 * @param dirPath 目录路径
 * @param fileDir 文件信息
 * @param zouts 输出流
 */
public static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) {
	if (fileDir.isDirectory()) {
		File[] files = fileDir.listFiles();
		// 空的文件夹
		if (files.length == 0) {
			// 目录信息
			ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));
			try {
				zouts.putNextEntry(entry);
				zouts.closeEntry();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}

		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				// 如果是文件,则调用文件压缩方法
				FileUtils
						.zipFilesToZipFile(dirPath, files[i], zouts);
			} else {
				// 如果是目录,则递归调用
				FileUtils.zipDirectoryToZipFile(dirPath, files[i],
						zouts);
			}
		}
	}
}
 
Example 5
Source File: FileHelper.java    From bird-java with MIT License 5 votes vote down vote up
/**
 * 递归压缩方法
 *
 * @param sourceFile       源文件
 * @param zos              zip输出流
 * @param name             压缩后的名称
 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
 *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
 * @throws Exception
 */
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception {
    byte[] buf = new byte[BUFFER_SIZE];
    if (sourceFile.isFile()) {
        // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
        zos.putNextEntry(new ZipEntry(name));
        // copy文件到zip输出流中
        int len;
        FileInputStream in = new FileInputStream(sourceFile);
        while ((len = in.read(buf)) != -1) {
            zos.write(buf, 0, len);
        }
        // Complete the entry
        zos.closeEntry();
        in.close();
    } else {
        File[] listFiles = sourceFile.listFiles();
        if (listFiles == null || listFiles.length == 0) {
            // 需要保留原来的文件结构时,需要对空文件夹进行处理
            if (KeepDirStructure) {
                // 空文件夹的处理
                zos.putNextEntry(new ZipEntry(name + "/"));
                // 没有文件,不需要文件的copy
                zos.closeEntry();
            }
        } else {
            for (File file : listFiles) {
                // 判断是否需要保留原来的文件结构
                if (KeepDirStructure) {
                    // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                    // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                    compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
                } else {
                    compress(file, zos, file.getName(), KeepDirStructure);
                }
            }
        }
    }
}
 
Example 6
Source File: ZipUtils.java    From joylau-springboot-daemon-windows with MIT License 5 votes vote down vote up
/**
 * <p>
 * 递归压缩文件
 * </p>
 *
 * @param parentFile parentFile
 * @param basePath basePath
 * @param zos zos
 */
private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception {
    File[] files = new File[0];
    if (parentFile.isDirectory()) {
        files = parentFile.listFiles();
    } else {
        files = new File[1];
        files[0] = parentFile;
    }
    String pathName;
    InputStream is;
    BufferedInputStream bis;
    byte[] cache = new byte[CACHE_SIZE];
    for (File file : files) {
        if (file.isDirectory()) {
            pathName = file.getPath().substring(basePath.length() + 1) + "/";
            zos.putNextEntry(new ZipEntry(pathName));
            zipFile(file, basePath, zos);
        } else {
            pathName = file.getPath().substring(basePath.length() + 1);
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            zos.putNextEntry(new ZipEntry(pathName));
            int nRead = 0;
            while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
                zos.write(cache, 0, nRead);
            }
            bis.close();
            is.close();
        }
    }
}
 
Example 7
Source File: FileUtils.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 将目录压缩到ZIP输出流
 * @param dirPath 目录路径
 * @param fileDir 文件信息
 * @param zouts 输出流
 */
public static void zipDirectoryToZipFile(String dirPath, File fileDir,
		ZipOutputStream zouts) {
	if (fileDir.isDirectory()) {
		File[] files = fileDir.listFiles();
		// 空的文件夹
		if (files.length == 0) {
			// 目录信息
			ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));
			try {
				zouts.putNextEntry(entry);
				zouts.closeEntry();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}

		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				// 如果是文件,则调用文件压缩方法
				FileUtils
						.zipFilesToZipFile(dirPath, files[i], zouts);
			} else {
				// 如果是目录,则递归调用
				FileUtils.zipDirectoryToZipFile(dirPath, files[i],
						zouts);
			}
		}

	}

}
 
Example 8
Source File: HttpDownHandler.java    From AndroidWebServ with Apache License 2.0 5 votes vote down vote up
/** 递归压缩文件进zip文件流 */
private void zip(ZipOutputStream zos, File file, String base) throws IOException {
    if (file.isDirectory()) { // 目录时
        File[] files = file.listFiles();
        if (null != files && files.length > 0) {
            for (File f : files) {
                zip(zos, f, base + "/" + f.getName()); // 递归
            }
        } else {
            zos.putNextEntry(new ZipEntry(base + "/")); // 加入目录条目
            zos.closeEntry();
        }
    } else {
        zos.putNextEntry(new ZipEntry(base)); // 加入文件条目
        FileInputStream fis = new FileInputStream(file); // 创建文件输入流
        try {
            int count; // 读取计数
            byte[] buffer = new byte[Config.BUFFER_LENGTH]; // 缓冲字节数组
            /* 写入zip输出流 */
            while ((count = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, count);
            }
        } finally {
            zos.flush();
            zos.closeEntry();
            fis.close();
        }
    }
}
 
Example 9
Source File: MigrateForm.java    From jeewx with Apache License 2.0 2 votes vote down vote up
/**
 * 创建目录
 * 
 * @param zos
 *            zip输出流
 * @param relativePath
 *            相对路径
 * @throws IOException
 */
private static void createZipNode(ZipOutputStream zos, String relativePath) throws IOException {
	ZipEntry zipEntry = new ZipEntry(relativePath);
	zos.putNextEntry(zipEntry);
	zos.closeEntry();
}
 
Example 10
Source File: MigrateForm.java    From jeecg with Apache License 2.0 2 votes vote down vote up
/**
 * 创建目录
 * 
 * @param zos
 *            zip输出流
 * @param relativePath
 *            相对路径
 * @throws IOException
 */
private static void createZipNode(ZipOutputStream zos, String relativePath) throws IOException {
	ZipEntry zipEntry = new ZipEntry(relativePath);
	zos.putNextEntry(zipEntry);
	zos.closeEntry();
}