zip壓縮/解壓縮帶空資料夾的檔案
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:[email protected]