我曾尝试使用此代码将 ZIP 文件转换为字节数组:
private static byte[] readZipFile(String zipFnm)
// read in fnm, returning it as a single string
{
FileInputStream fileInputStream=null;
File file = new File(zipFnm);
byte[] bFile = new byte[(int) file.length()];
try{
//convert file into array of bytes
fileInputStream = new FileInputStream(zipFnm);
fileInputStream.read(bFile);
fileInputStream.close();
}catch(Exception e){
e.printStackTrace();
}
return bFile;
}
Run Code Online (Sandbox Code Playgroud)
以及用于将字节数组转换回 ZIP 的代码,通过调用 writeByteToZip(fnm + ".zip");
private static String writeByteToZip(String outFnm)
{
try {
FileOutputStream fileOuputStream = new FileOutputStream(outFnm);
fileOuputStream.write(bFile);
fileOuputStream.close();
} catch ( IOException iox ){
iox.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 以下代码是将int转换为Bytes数组.我知道int i右移24,16,8次,ANDED用0xFF但是我无法理解的是为什么使用这些数字?
private static byte[] intToBytes(int i)
// split integer i into 4 byte array
{
// map the parts of the integer to a byte array
byte[] integerBs = new byte[4];
integerBs[0] = (byte) ((i >>> 24) & 0xFF);
integerBs[1] = (byte) ((i >>> 16) & 0xFF);
integerBs[2] = (byte) ((i >>> 8) & 0xFF);
integerBs[3] = (byte) (i & 0xFF);
// for (int j=0; j < integerBs.length; j++)
// System.out.println(" integerBs[ " + j + "]: " …Run Code Online (Sandbox Code Playgroud)