zip压缩/解压缩带空文件夹的文件
2011年07月21日 20:04:54 flex_work 阅读数:7152 标签: filestringbytebuffernullinclude 更多
个人分类: Flex And Java
在EncryptZip的项目中对Zip的加密/解密没有包含带子文件夹的需求, 只有一层的关系.
在此文中给出普通的zip压缩/解压缩的Java代码, 压缩时递归压缩文件,包含文件及文件下的空文件夹. 若Flex端需要类似的功能, 可以参考本文中的zipFileWithTier 和unZipFileWithTier方法.
代码如下:
-
public class ZipFileWithTier { -
private static final String zipPath = "C:\\temp\\Lemur\\empty.zip"; -
private static final String unzipPath = "C:\\temp\\Lemur\\"; -
private static final String srcFiles = "C:\\temp\\Lemur\\empty"; -
@Test -
public void zipFile(){ -
File file = new File(zipPath); -
if(file.exists()) -
file.delete(); -
zipFileWithTier(srcFiles, zipPath); -
} -
@Test -
public void upZipFile(){ -
try { -
unzipFilesWithTier(readFileByte(zipPath), unzipPath + File.separator); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
} -
/* -
* Compress the specify file that contains sub-folders and store them to a zipfile. -
* @param srcFiles: the file will be compressed -
* @param zipPath: the location which stores the zipfile. -
*/ -
public static void zipFileWithTier(String srcFiles, String zipPath) { -
try { -
FileOutputStream zipFile = new FileOutputStream(zipPath); -
BufferedOutputStream buffer = new BufferedOutputStream(zipFile); -
ZipOutputStream out = new ZipOutputStream(buffer); -
zipFiles(srcFiles, out, ""); -
out.close(); -
} catch (IOException e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} -
} -
/* -
* Recursive the specify file also contains the folder which may don't include any file. -
* @param filePath: compress file -
* @param ZipOutputStream: the zipfile's outputStream. -
* @param prefix: the prefix indicates the parent folder name of the file that makes the tier relation. -
*/ -
public static void zipFiles(String filePath, ZipOutputStream out, String prefix) -
throws IOException { -
File file = new File(filePath); -
if (file.isDirectory()) { -
if (file.listFiles().length == 0) { -
ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/"); -
out.putNextEntry(zipEntry); -
out.closeEntry(); -
} else { -
prefix += file.getName() + File.separator; -
for (File f : file.listFiles()) -
zipFiles(f.getAbsolutePath(), out, prefix); -
} -
} else { -
FileInputStream in = new FileInputStream(file); -
ZipEntry zipEntry = new ZipEntry(prefix + file.getName()); -
out.putNextEntry(zipEntry); -
byte[] buf = new byte[1024]; -
int len; -
while ((len = in.read(buf)) > 0) { -
out.write(buf, 0, len); -
} -
out.closeEntry(); -
in.close(); -
} -
} -
/* -
* Unzip the file also contains the folder which the listFiles's length is 0. -
* @param bytes: the content of the zipfile by byte array. * -
* @param prefix: the prefix is the root of the store path. -
* @IOExcetion: the ioexception during unzipFiles. -
*/ -
public static void unzipFilesWithTier(byte[] bytes, String prefix) throws IOException { -
InputStream bais = new ByteArrayInputStream(bytes); -
ZipInputStream zin = new ZipInputStream(bais); -
ZipEntry ze; -
while ((ze = zin.getNextEntry()) != null) { -
if (ze.isDirectory()) { -
File file = new File(prefix + ze.getName()); -
if (!file.exists()) -
file.mkdirs(); -
continue; -
} -
File file = new File(prefix + ze.getName()); -
if (!file.getParentFile().exists()) -
file.getParentFile().mkdirs(); -
ByteArrayOutputStream toScan = new ByteArrayOutputStream(); -
byte[] buf = new byte[1024]; -
int len; -
while ((len = zin.read(buf)) > 0) { -
toScan.write(buf, 0, len); -
} -
byte[] fileOut = toScan.toByteArray(); -
toScan.close(); -
writeByteFile(fileOut, new File(prefix + ze.getName())); -
} -
zin.close(); -
bais.close(); -
} -
public static byte[] readFileByte(String filename) throws IOException { -
if (filename == null || filename.equals("")) { -
throw new NullPointerException("File is not exist!"); -
} -
File file = new File(filename); -
long len = file.length(); -
byte[] bytes = new byte[(int) len]; -
BufferedInputStream bufferedInputStream = new BufferedInputStream( -
new FileInputStream(file)); -
int r = bufferedInputStream.read(bytes); -
if (r != len) -
throw new IOException("Read file failure!"); -
bufferedInputStream.close(); -
return bytes; -
} -
public static String writeByteFile(byte[] bytes, File file) { -
FileOutputStream fos = null; -
try { -
fos = new FileOutputStream(file); -
fos.write(bytes); -
} catch (FileNotFoundException e) { -
e.printStackTrace(); -
} catch (IOException e) { -
e.printStackTrace(); -
} finally { -
if (fos != null) { -
try { -
fos.close(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
} -
} -
return "success"; -
} -
}
(转贴请注明出处)
Author:David
Mail:xiang.okay@gmail.com
![]()
nbhaohao: 谢谢博主,学习了(1年前#1楼)举报回复
本文提供了一段Java代码,用于实现ZIP文件的压缩与解压缩功能,特别强调了如何处理和保留文件结构中的空文件夹。通过递归方式,该方法能够完整地保存目录层级,确保在解压缩后文件夹结构得以还原。
687

被折叠的 条评论
为什么被折叠?



