package com.vci.server.volume.uitls;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.util.Arrays;
|
import java.util.zip.CRC32;
|
|
import org.apache.axis.utils.StringUtils;
|
|
public class EncryFileUtil {
|
public static int ENCRYHEADERSIZE = 13;
|
|
public static boolean isEncryFile(String fileName) {
|
File file = new File(fileName);
|
if (!file.exists())
|
return false;
|
|
if (file.length() < ENCRYHEADERSIZE)
|
return false;
|
|
try {
|
FileInputStream in = new FileInputStream(file);
|
byte[] data = new byte[ENCRYHEADERSIZE];
|
in.read(data, 0, ENCRYHEADERSIZE);
|
in.close();
|
|
byte[] header = getEncryHeader(fileName);
|
return Arrays.equals(data, header);
|
|
} catch (IOException e) {
|
return false;
|
}
|
}
|
|
public static boolean isEncryFile(File file) {
|
if (!file.exists())
|
return false;
|
|
if (file.length() < ENCRYHEADERSIZE)
|
return false;
|
|
try {
|
FileInputStream in = new FileInputStream(file);
|
byte[] data = new byte[ENCRYHEADERSIZE];
|
in.read(data, 0, ENCRYHEADERSIZE);
|
in.close();
|
|
|
byte[] header = getEncryHeader(file.getPath());
|
return Arrays.equals(data, header);
|
|
} catch (IOException e) {
|
return false;
|
}
|
}
|
|
public static byte[] getEncryHeader(String fileName) {
|
if (StringUtils.isEmpty(fileName))
|
return null;
|
|
long crc = 0;
|
File file = new File(fileName);
|
String name = file.getName().toLowerCase();
|
crc = getCRC(name);
|
|
// 先写入“VEH1#”标识
|
byte[] data = new byte[ENCRYHEADERSIZE];
|
data[0] = (byte)'V';
|
data[1] = (byte)'E';
|
data[2] = (byte)'H';
|
data[3] = (byte)'1';
|
data[4] = (byte)'#';
|
|
long temp = crc;
|
for (int i = 0; i < 8; i++) {
|
data[i + 5] = new Long(temp & 0xff).byteValue();//
|
temp = temp >> 8; // 向右移8位
|
}
|
|
|
return data;
|
}
|
|
/**
|
* 计算CRC16校验码
|
*
|
* @param bytes
|
* @return
|
*/
|
private static long getCRC(String text) {
|
byte[] b = text.getBytes();
|
CRC32 c = new CRC32();
|
c.reset();//Resets CRC-32 to initial value.
|
c.update(b, 0, b.length);//将数据丢入CRC32解码器
|
long value = (int) c.getValue();//获取CRC32 的值 默认返回值类型为long 用于保证返回值是一个正数
|
|
return value;
|
}
|
}
|