All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.common.FileUtils Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.common;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileUtils {
	
	public final static String FORMAT_GIF = "gif";
	public final static String FORMAT_JPG = "jpg";
	public final static String FORMAT_PNG = "png";
	public final static String FORMAT_PDF = "pdf";
	public final static String FORMAT_XLS = "xls";
	public final static String FORMAT_XLSX = "xlsx";
	public final static String FORMAT_DOC = "doc";
	public final static String FORMAT_DOCX = "docx";
	public final static String FORMAT_XLS_DOC = "doc/xls";
	public final static String FORMAT_XLSX_DOCX = "docx/xlsx";
	public final static String FORMAT_TXT = "txt";

	private final static byte[] JPGID = {
			(byte)0xFF, (byte)0xD8};
	private final static byte[] PNGID = {
			(byte)0x89, (byte)0x50, (byte)0x4E, (byte)0x47};
	private final static byte[] PDFID = {
			(byte)0x25, (byte)0x50, (byte)0x44, (byte)0x46, (byte)0x2D,
			(byte)0x31, (byte)0x2E};
	private final static byte[] XLS_DOCID = {
			(byte)0xD0, (byte)0xCF, (byte)0x11, (byte)0xE0};
	private final static byte[] XLSX_DOCXID = {
			(byte)0x50, (byte)0x4B, (byte)0x03, (byte)0x04};
	/*private final static byte[] XLSX_DOCXID = {
			(byte)0x50, (byte)0x4B, (byte)0x03, (byte)0x04, (byte)0x14,
			(byte)0x00, (byte)0x06, (byte)0x00, (byte)0x08, (byte)0x00,
			(byte)0x00, (byte)0x00, (byte)0x21, (byte)0x00};*/
	private final static byte[] DOCID512 = {
			(byte)0xEC, (byte)0xA5, (byte)0xC1, (byte)0x00};
	private final static byte[] RTFID = {
			(byte)0x7B, (byte)0x5C, (byte)0x72, (byte)0x74, (byte)0x66};
	
	/* ?????ļ????ݼ??ͼƬ??ʽ??֧??gif??jpg??png??????ʶʱ????null??*/
	public static String getPicFormat(byte[] data) {
        if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
        	//GIFͼƬ
        	return FileUtils.FORMAT_GIF;
        }
        else if (check(data, JPGID)) {
        	return FileUtils.FORMAT_JPG;
        }
        else if (data[1] == PNGID[1] && data[2] == PNGID[2] &&
          data[3] == PNGID[3]) {
        	return FileUtils.FORMAT_PNG;
        }
		return null;
	}
	
	private static boolean check(byte[] data, byte[] id) {
		int len = id.length;
		if (data.length < len) return false;
		for (int i=0; i= offset; j--)
        {
            result <<= 8;
            result |= 0xff & data[j];
        }

        return result;
    }

    private static int getInt(byte data[], int offset)
    {
        return (int)getNumber(data, offset, 4);
    }

    private static short getShort(byte data[], int offset)
    {
        return (short)getNumber(data, offset, 2);
    }
	
	public static void main(String[] arg) {
		//String filepath = "D:/files/DummyData.xlsx";
		String filepath = "D:/files/test.docx";
		//String filepath = "D:/files/case(B).doc";
		//String filepath = "D:/files/3.xls";
		try {
			com.scudata.dm.FileObject fo = new com.scudata.dm.FileObject(filepath);
			byte[] bytes = (byte[]) fo.read(0, -1, "b");
			String type = FileUtils.getFileFormat(bytes);
			System.out.println(type);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy