Java Can';我不读ZIpInputStream
我在将ZipInputStream读取到字节数组时遇到了一些问题-结果是-1。但是,我可以将其读取为文件流,因此我不确定是否遗漏了某些内容:Java Can';我不读ZIpInputStream,java,Java,我在将ZipInputStream读取到字节数组时遇到了一些问题-结果是-1。但是,我可以将其读取为文件流,因此我不确定是否遗漏了某些内容: public void readContent () throws IOException { int size = 2048; byte[] file = new byte[size]; byte[] stream = new byte[size]; FileInpu
public void readContent () throws IOException {
int size = 2048;
byte[] file = new byte[size];
byte[] stream = new byte[size];
FileInputStream fileStream = new FileInputStream(this.file);
ZipInputStream zipStream = new ZipInputStream(fileStream);;
System.out.println("Reading as a file stream " + fileStream.read(file,0,size)); //output 2048
System.out.println("Reading as a zip stream " + zipStream.read(stream,0,size)); // output -1
}
应通过
ZipEntry
读取来自ZipInputStream
的数据。这是一个如何从给定的InputStream
读取zip归档文件,并使用key-full-zip条目路径、value-zip条目数据检索Map
:
public class ZipUtils {
private static final Comparator<String> COMPARATOR_STRING_DESC = (str1, str2) -> str1 == null ? 1 : str2 == null ? -1 : -str1.compareTo(str2);
public static Map<String, byte[]> unzipIt(InputStream is) throws IOException {
try (ZipInputStream in = new ZipInputStream(is)) {
ZipEntry entry;
Map<String, byte[]> content = new HashMap<>();
Set<String> dirs = new TreeSet<>(COMPARATOR_STRING_DESC);
while ((entry = in.getNextEntry()) != null) {
String path = removeDirectoryMarker(replaceIncorrectFileSeparators(entry.getName()));
if (isDirectory(entry)) {
dirs.add(path);
} else {
content.put(path, IOUtils.toByteArray(in));
}
}
addOnlyEmptyDirectories(dirs, content);
return content.isEmpty() ? Collections.emptyMap() : content;
}
}
private static boolean isDirectory(ZipEntry entry) {
return entry.isDirectory() || entry.getName().endsWith(ILLEGAL_DIR_MARKER);
}
private static void addOnlyEmptyDirectories(Set<String> dirs, Map<String, byte[]> content) {
if (dirs.isEmpty()) {
return;
}
Set<String> paths = new HashSet<>(content.keySet());
for (String dir : dirs) {
boolean empty = true;
for (String path : paths) {
if (path.startsWith(dir)) {
empty = false;
break;
}
}
if (empty) {
content.put(dir, null);
}
}
}
private static final String DIR_MARKER = "/";
private static final String ILLEGAL_DIR_MARKER = "\\";
private static final java.util.regex.Pattern BACK_SLASH = Pattern.compile("\\\\");
private static String removeDirectoryMarker(String path) {
return path.endsWith(DIR_MARKER) || path.endsWith(ILLEGAL_DIR_MARKER) ? path.substring(0, path.length() - 1) : path;
}
private static String replaceIncorrectFileSeparators(String path) {
return BACK_SLASH.matcher(path).replaceAll(DIR_MARKER);
}
}
公共类ZipUtils{
私有静态最终比较器比较器\u STRING\u DESC=(str1,str2)->str1==null?1:str2==null?-1:-str1.compareTo(str2);
公共静态映射unzipIt(InputStream is)引发IOException{
try(ZipInputStream in=新的ZipInputStream(is)){
ZipEntry入口;
映射内容=新的HashMap();
Set dirs=新树集(比较器字符串描述);
while((entry=in.getNextEntry())!=null){
字符串路径=removeDirectoryMarker(replaceIncorrectFileSeparators(entry.getName());
if(isDirectory(条目)){
目录添加(路径);
}否则{
put(路径,IOUtils.toByteArray(in));
}
}
addOnlyEmptyDirectories(目录、内容);
return content.isEmpty()?Collections.emptyMap():content;
}
}
专用静态布尔isDirectory(ZipEntry条目){
return entry.isDirectory()|| entry.getName().endsWith(非法的_DIR_标记);
}
私有静态void addonlymptydirectories(设置目录、映射内容){
if(dirs.isEmpty()){
返回;
}
Set path=newhashset(content.keySet());
for(字符串目录:目录){
布尔空=真;
用于(字符串路径:路径){
if(路径起始带(目录)){
空=假;
打破
}
}
if(空){
content.put(dir,null);
}
}
}
私有静态最终字符串DIR_MARKER=“/”;
私有静态最终字符串非法\u DIR\u MARKER=“\\”;
私有静态final java.util.regex.Pattern BACK\u SLASH=Pattern.compile(“\\\\”);
私有静态字符串removeDirectoryMarker(字符串路径){
返回path.endsWith(DIR_标记)| | path.endsWith(非法的DIR_标记)→path.substring(0,path.length()-1):path;
}
私有静态字符串替换不正确的文件分隔符(字符串路径){
返回斜杠.matcher(路径).replaceAll(方向标记);
}
}
ZipInputStream需要使用zipStream.getNextEntry()
将ZipInputStream“指向”特定的ZipEntry
,然后才能提供任何数据。(如果你环顾四周,有很多关于这方面的工作示例。)