RedCollarPanda
RedCollarPanda

Reputation: 1481

Get byte [] from file in ZIP

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.

Upvotes: 0

Views: 3008

Answers (3)

Dhwanil Patel
Dhwanil Patel

Reputation: 2573

Here first we can covert our zip file to simple file object. Apache commons library IOUtils have in-built function for do same stuff.

ZipFile zipFile = new ZipFile(convertMultiPartToFile(file)
byte[] bytes = IOUtils.readAllBytes(zipFile.getInputStream(entry));

Upvotes: 0

devilpreet
devilpreet

Reputation: 767

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

Upvotes: 2

user207421
user207421

Reputation: 310860

BufferedReader.toString() doesnt perform any I/O. If you want the contents of the stream you have to read them. BufferedReader isn't appropriate anyway unless you know the file is text.

Upvotes: 0

Related Questions