0
荏苒文件和文件夾我希望在Java中創建一個zip程序,它zip文件和文件夾,讓說結構是這樣 -zip4j在Java
- 文件夾,一個/
- 文件夾,1個/ 1 .TXT
- 夾酮/ two.mp3
- 夾酮/ three.jpg
- 夾個/
- 夾個/ four.doc
- 文件夾個/ five.rtf
- 文件夾兩/文件夾三/
- 文件夾兩/文件夾三/ six.txt
我用zip4j開源的,我已經收集了所有在一個列表文件(絕對路徑),然後給它來壓縮,但它是文件壓縮和解僅在my.zip -
- 此時就把one.txt存盤
- two.mp3
- three.jpg
- four.doc
- five.rtf
- six.txt
我怎麼能保持在壓縮和解壓,因爲它是當地較早相同的結構。請建議其他開放源代碼是否可以幫助我像使用其他Windows壓縮程序一樣壓縮/解壓縮相同的結構文件和文件夾。
代碼如下 -
public class CreateZipWithOutputStreams {
ArrayList filesToAdd = new ArrayList();
public void CreateZipWithOutputStreams(String sAbsolutePath) {
ZipOutputStream outputStream = null;
InputStream inputStream = null;
try {
ArrayList arrLocal = exploredFolder(sAbsolutePath);
outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\\ZipTest\\CreateZipFileWithOutputStreams.zip")));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("neelam");
for (int i = 0; i < arrLocal.size(); i++) {
File file = (File) arrLocal.get(i);
outputStream.putNextEntry(file, parameters);
if (file.isDirectory()) {
outputStream.closeEntry();
continue;
}
inputStream = new FileInputStream(file);
byte[] readBuff = new byte[4096];
int readLen = -1;
while ((readLen = inputStream.read(readBuff)) != -1) {
outputStream.write(readBuff, 0, readLen);
}
outputStream.closeEntry();
inputStream.close();
}
outputStream.finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public ArrayList exploredFolder(String sAbsolutePath) {
File[] sfiles;
File fsSelectedPath = new File(sAbsolutePath);
sfiles = fsSelectedPath.listFiles();
if (sfiles == null) {
return null;
}
for (int j = 0; j < sfiles.length; j++) {
File f = sfiles[j];
if (f.isDirectory() == true) {
exploredFolder(f.getAbsolutePath());
} else {
filesToAdd.add(f);
}
}
return filesToAdd;
}
public static void main(String[] args) {
new CreateZipWithOutputStreams().CreateZipWithOutputStreams("c:\\ZipTest");
}
}
謝謝!
感謝您的回覆!我必須使用zip4j的zipFile.createZipFileFromFolder(sAbsolutePath,參數,true,10485760)方法。現在它的工作很好。你能否讓我知道驗證壓縮文件的方式是否正確壓縮了準確的內容? –
@NeelamSharma我認爲你需要關閉這個問題並打開一個新的問題:)在新的問題中,讓我知道你到底想要驗證什麼。某些文件的存在? b。它是否腐敗? –
http://stackoverflow.com/questions/12565323/validate-zip-file-in-java –