????zip????????????

ZIP??????????ZIP??????????????????????????zip??????????????????????????????????????????

??????????
outer.zip
 +first.txt
 +inner.zip
 � +game.exe
 � +subitem.bin
 +picture.gif
???????????
flatten.zip
 +first.txt
 +picture.gif
 +game.exe
 +subitem.bin

Asopsion.Zip??????????? Zip Archive??????[??]???????????

????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????

????????????????????????????????????????????????? ????????????????????????????????????????????????????

1if(entry.getname()?tolowercase(locale.root).endswith( "?zip")){
2    entriestodelete.add(entry);
3    ...
4}

????????????????????

Aspose.zip?????????????????writable???????zip???????????????????????????????????????????????????

??:?????????????????????????????????????????????

1byte [] b = new byte [8192];
2int bytesRead;
3inputStream entrystream = entry.open();
4bytearrayoutputStream innerCompressed = new bytearrayoutputStream();
5while(0 <(bytesread = entrystream.read(b?0?b.length))){
6    innerCompressed.write(b?0?bytesread);
7}

????????????????????????????????????? ??????????????????????????????????? ??????????????????????:

1Archive Inner = new Archive(new bytearrayinputStream(innerCompressed.tobytearray()));

???????

???????????zip???????????????????

1for(archiveentry e:entriestodelete){
2    outer.deleteentry(e);
3}

????????

???????????????

 1try(?????outer = new Archive( "outer.zip")){
 2    arrayList <ArchiveEntry> entriestodelete = new ArrayList <ArchiveEntry>();
 3    arrayList <string> namestoinsert = new ArrayList <String>();
 4    arrayList <inuptStream> contentToInsert = new ArrayList <inputStream>();
 5    for(archiveentry entry:outer.getentries()){
 6        //????????????????????
 7        if(entry.getname()?tolowercase(locale.root).endswith( "?zip")){
 8            //??????????????????????????????
 9            entriestodelete.add(entry);
10
11            //This extracts the entry to a memory stream
12            byte[] b = new byte[8192];
13            int bytesRead;
14            InputStream entryStream = entry.open();
15            ByteArrayOutputStream innerCompressed = new ByteArrayOutputStream();
16            while (0 < (bytesRead = entryStream.read(b, 0, b.length))) {
17                innerCompressed.write(b, 0, bytesRead);
18            }
19
20            // We know that content of the entry is a zip archive, so we may extract
21            try(Archive inner = new Archive(new ByteArrayInputStream(innerCompressed.toByteArray()))) {
22
23                // Loop over entries of inner archive
24                for(ArchiveEntry ie : inner.getEntries()) {
25
26                    // Keep the name of inner entry.
27                    namesToInsert.add(ie.getName());
28
29                    InputStream ieStream = ie.open();
30                    ByteArrayOutputStream content = new ByteArrayOutputStream();
31                    while (0 < (bytesRead = ieStream.read(b, 0, b.length))) {
32                        content.write(b, 0, bytesRead);
33                    }
34
35                    // Keep the content of inner entry.
36                    contentToInsert.add(new ByteArrayInputStream(content.toByteArray()));
37                }
38            }
39        }
40    }
41
42    for(ArchiveEntry e : entriesToDelete) {
43        // Delete all the entries which are archives itself
44        outer.deleteEntry(e);
45    }
46
47    for(int i = 0; i < namesToInsert.size(); i++) {
48        // Adds entries which were entries of inner archives
49        outer.createEntry(namesToInsert.get(i), contentToInsert.get(i));
50    }
51
52    outer.save("flatten.zip");
53} catch(??Ex){
54    System.out.println(ex);
55}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.