archive file in image

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by galaxyAbstractor, Oct 2, 2009.

  1. ah-blabla

    ah-blabla New Member

    Messages:
    375
    Likes Received:
    7
    Trophy Points:
    0
    Doh... I wasn't thinking clearly... But you got it to work, so aim achieved.;)
     
  2. galaxyAbstractor

    galaxyAbstractor Community Advocate Community Support

    Messages:
    5,508
    Likes Received:
    35
    Trophy Points:
    48
    k I'm getting some weird issue again :/

    Code:
    public boolean isArchive(String input){
    
                            InputStream inFile = null;
                            //Initialise the value of 'valid' to false
                            boolean valid = false;
                            //This is the signature every file should have
                            int[] filesigrar = {0x58, 0x59, 0x12, 0xA2};
                            int[] filesigzip = {0x58, 0x59, 0x12, 0xA3};
                            try {
                                inFile = new FileInputStream(input);
                                //Go to the last 4 bytes
                                inFile.skip(input.length() - 4);
                                //Compare each of the last 4 bytes to the expected signatures
                                for(int i=0; i<4; i++) {
                                    int c = inFile.read();
                                    if(c == filesigrar[i]) {
                                        valid = true;
    
                                    } else if(c == filesigzip[i]) {
                                        valid = true;
    
                                    } else {
                                        //In case any of the 4 bytes fails the check, the file is invalid
                                        //and the loop is aborted.
                                        valid = false;
                                        break;
                                    }
                                }
                            //Any exception assumes that the input is invalid.
                            } catch (FileNotFoundException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                    valid = false;
                            } catch (IOException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                    valid = false;
                            }
                            System.out.println(valid);
                            return valid;
        }
    
    it will return false even if the image contains a rar/zip. It's weird as same code is used to search for the archive in a Thread, and there it works :s
     
  3. marshian

    marshian New Member

    Messages:
    526
    Likes Received:
    9
    Trophy Points:
    0
    Sorry I'm really busy lately, so I won't debug your code for you. (Maybe some other time.)
    Try finding out where it goes wrong. Put System.out.println's where the boolean can be set to false and find out which part of the algorithm causes valid to be false. Then you can focus on that area and you'll probably find the bug easily.

    Good luck :)
     
  4. galaxyAbstractor

    galaxyAbstractor Community Advocate Community Support

    Messages:
    5,508
    Likes Received:
    35
    Trophy Points:
    48
    oh I fail, I skipped the length of the string, not the file xD. Now it works
     
  5. galaxyAbstractor

    galaxyAbstractor Community Advocate Community Support

    Messages:
    5,508
    Likes Received:
    35
    Trophy Points:
    48

Share This Page