Results 1 to 3 of 3
  1. #1
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default how do you get the file type of say a .ZIP file

    whats a good method of checking the file is a zip file, apart from checking the extension (toLower)

    checking the mime type didn't work as it gave application/octect (as did word docs)

    I need to make sure file type is zip, before I do anything with it there are many good java resources for zip, but cant find a good file type check



    heres my mime method that didn't work: (or showed there wasn't a difference between mime types for zip files and word files)

    PHP Code:
    private void listAllzipfiles(String folderlocation){    
    		    
        File folder = new File(folderlocation);
        File[] listOfFiles = folder.listFiles();
        
        for (int i = 0; i < listOfFiles.length; i++) {
          
    	    
    	    if (listOfFiles[i].isFile()) {
            File file = new File(listOfFiles[i].getName());
            
    		System.out.println("File " + listOfFiles[i].getName());
            String MimeTypeOfFile = new MimetypesFileTypeMap().getContentType(file);
            System.out.println("Type " + MimeTypeOfFile);
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }

  2. #2
    Mr tuition is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    I just made a check extension instead (should work)

    PHP Code:
        public void CheckExtension(String Extension, String thefile) {
        // eg (extension will be 'zip', file will be 'myFile.zip'
        Extension = Extension.toLowerCase();    
        thefile = thefile.toLowerCase(); 
        //if the file ends with the extension
        int extensionStart = thefile.lastIndexOf('.'+ Extension);
        int ExtensionEnd = Extension.length() + extensionStart +1; 
       if ((thefile.lastIndexOf('.'+Extension) > -1 )&& (ExtensionEnd == thefile.length())){
    	   FileType = true;	    
    	    }else { FileType = false;}	    
    	}
    I thought there might be a simple file.getType() or something;
    Last edited by Mr tuition; 12-18-2007 at 02:38 PM.

  3. #3
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    Default

    you could try to create a ZipFile object with the file, maybe it would barf if it was not a valid ZIP file.

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. Replies: 2
    Last Post: 04-07-2009, 07:25 AM
  3. Mime type of a file from web
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:20 PM
  4. Mime type of a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:18 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •