I nees to get a file from *.zip as byte array. I do this
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());
}
lets say, that I get
[B@8f2ef19
but when I unzip file and do :
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);
I get :
[B@2cf3d63b
What's wrong?
Besides if I do last fragment of code several times - I always ged different results.
You can use the following code to get the Bytes or the String :
public static void main(String[] args) throws IOException {
ZipFile zipFile = new ZipFile("C:/Test/Test.zip");
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
System.out.println(entry.getName());
InputStream stream = zipFile.getInputStream(entry);
//For characters
//BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
BufferedInputStream reader = new BufferedInputStream(stream);
//For line reading
//System.out.println(reader.readLine());
int byteRead = reader.read();
while(byteRead != -1) {
System.out.println(byteRead);
byteRead = reader.read();
}
}
}
The one you are seeing is simply the object that is instantiated every time you run it and toString represents the unsigned hexadecimal representation of the hash code of the object