我需要从* .zip获取文件作为字节数组。我这样做从ZIP文件获取字节[]
String zipFileLocation = pathToFile;
File file = new File(zipFileLocation);
ZipFile zipfile = new ZipFile(file);
ZipEntry zipentry;
System.out.println("nList of files in zip archive");
int fileNumber = 0;
for (Enumeration<? extends ZipEntry> e = zipfile.entries();
e.hasMoreElements(); fileNumber++) {
zipentry = e.nextElement();
if (!zipentry.isDirectory()) {
System.out.println(fileNumber + "-" + zipentry.getName());
}
}
for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
zipentry = e.nextElement();
if (!zipentry.isDirectory()) {
fileName = zipentry.getName();
InputStream input = zipfile.getInputStream(zipentry);
BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
(hash map) nameANDbytes.put(fileName, br.toString().getBytes());
}
可以说,我得到
[[email protected]
但是当我解压缩文件,然后执行:
String pathToCheckingFile = "path_to_file";
byte[] b = {};
try {
RandomAccessFile f = new RandomAccessFile(pathToCheckingFile, "r");
b = new byte[(int) f.length()];
f.read(b);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
System.out.println(b);
我得到:
[[email protected]
什么问题?
此外,如果我做了几次代码片段 - 我总是ged不同的结果。
它们指向不同的字节数组,即全部。你应该检查两个数组的实际内容是什么。 – Tom 2014-10-19 10:01:50