如何創建壓縮文件 enter image description here如何創建zip內其他zip.i得到FileNotFoundException異常
拉鍊內得到了波紋管錯誤:
D:\sagar\my work\Package Maker\DirectoryStruct>java zipStructure
java.io.FileNotFoundException: Additional_Sub_Folder\Additional_file.zip (The sy
stem cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at zipStructure.main(zipStructure.java:22)
D:\sagar\my work\Package Maker\DirectoryStruct>
是Additional_sub_folder和Digital_sub_folder。內部產生(Additional_file.zip)Additional_sub_folder 1個zip文件,即拉鍊2文件夾內創建,如XML文件夾和pdf文件夾
和內部Digital_sub_folder創建Artical_sub_folder,和內部Artical_sub_folder 3所創建的XML是文件夾的新文件夾,PDF格式的文件夾和圖形文件夾。我嘗試在Java代碼下面,但不能正常工作,請幫助創建這個結構。
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class zipStructure {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("main.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry("Additional_Sub_Folder/"));
zos.putNextEntry(new ZipEntry("Digital_Sub_Folder/"));
FileOutputStream fos1 = new FileOutputStream("Additional_Sub_Folder/Additional_file.zip");
ZipOutputStream zos1 = new ZipOutputStream(fos1);
/*zos1.putNextEntry(new ZipEntry("Additional_file.zip/xml/"));
zos1.putNextEntry(new ZipEntry("Additional_file.zip/pdf/"));*/
zos1.putNextEntry(new ZipEntry("Additional_file1.zip/xml/"));
zos1.putNextEntry(new ZipEntry("Additional_file1.zip/pdf/"));
zos1.close();
fos1.close();
zos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
}
[1]: http://i.stack.imgur.com/TNA8b.jpg
這裏我動態地創建了文件夾和zip文件,但是每當我嘗試在Additional_Sub_Folder文件夾內創建Additional_file.zip文件,那麼我得到了這個錯誤,請檢查我提供的圖像,這就是我想要的結構。 –
不能使用eclipse,我試着在記事本上運行命令提示符 –
現在我唯一認識到的就是在fos1中你錯過了main.zip的路徑......但我不確定它是否需要。 FileOutputStream fos1 = new FileOutputStream(「MAIN1.zip ??? Additional_Sub_Folder/Additional_file.zip」); –