keep sorting with original zip entry

This commit is contained in:
REAndroid 2023-03-12 14:00:47 -04:00
parent 4e9cfd4100
commit 9914722bac
2 changed files with 41 additions and 9 deletions

View File

@ -15,12 +15,13 @@
*/
package com.reandroid.archive;
import java.io.File;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class InputSourceUtil {
public class InputSourceUtil {
public static String toRelative(File rootDir, File file){
int len=rootDir.getAbsolutePath().length();
@ -37,14 +38,11 @@ public class InputSourceUtil {
while (path.startsWith("/")){
path=path.substring(1);
}
while (path.startsWith(File.separator)){
path=path.substring(1);
}
return path;
}
public static Map<String, InputSource> mapZipFileSources(ZipFile zipFile){
Map<String, InputSource> results=new HashMap<>();
Map<String, InputSource> results=new LinkedHashMap<>();
Enumeration<? extends ZipEntry> entriesEnum = zipFile.entries();
int i=0;
while (entriesEnum.hasMoreElements()){
@ -54,11 +52,42 @@ public class InputSourceUtil {
}
ZipEntrySource source=new ZipEntrySource(zipFile, zipEntry);
source.setSort(i);
source.setMethod(zipEntry.getMethod());
results.put(source.getName(), source);
i++;
}
return results;
}
public static Map<String, ByteInputSource> mapInputStreamAsBuffer(InputStream inputStream) throws IOException {
Map<String, ByteInputSource> results = new LinkedHashMap<>();
ZipInputStream zin = new ZipInputStream(inputStream);
ZipEntry zipEntry;
int i=0;
while ((zipEntry=zin.getNextEntry())!=null){
if(zipEntry.isDirectory()){
continue;
}
byte[] buffer = loadBuffer(zin);
String name = sanitize(zipEntry.getName());
ByteInputSource source = new ByteInputSource(buffer, name);
source.setSort(i);
source.setMethod(zipEntry.getMethod());
results.put(name, source);
i++;
}
zin.close();
return results;
}
private static byte[] loadBuffer(InputStream in) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buff=new byte[40960];
int len;
while((len=in.read(buff))>0){
outputStream.write(buff, 0, len);
}
outputStream.close();
return outputStream.toByteArray();
}
public static List<InputSource> listZipFileSources(ZipFile zipFile){
List<InputSource> results=new ArrayList<>();
Enumeration<? extends ZipEntry> entriesEnum = zipFile.entries();

View File

@ -30,7 +30,7 @@ public class ZipArchive {
this.mEntriesMap=entriesMap;
}
public ZipArchive(){
this(new HashMap<>());
this(new LinkedHashMap<>());
}
public void extract(File outDir) throws IOException {
@ -95,19 +95,22 @@ public class ZipArchive {
List<InputSource> sourceList = InputSourceUtil.listZipFileSources(zipFile);
this.addAll(sourceList);
}
public void addAll(Collection<InputSource> inputSourceList){
public void addAll(Collection<? extends InputSource> inputSourceList){
for(InputSource inputSource:inputSourceList){
add(inputSource);
}
}
public void add(InputSource inputSource){
if(inputSource==null){
return;
}
String name=inputSource.getName();
Map<String, InputSource> map=mEntriesMap;
map.remove(name);
map.put(name, inputSource);
}
public List<InputSource> listInputSources(){
return InputSourceUtil.sort(new ArrayList<>(mEntriesMap.values()));
return new ArrayList<>(mEntriesMap.values());
}
public InputSource getInputSource(String name){
return mEntriesMap.get(name);