小编Lai*_*lot的帖子

如何将 zip 文件读取为字节数组,然后将字节数组转换回 zip 文件?

我曾尝试使用此代码将 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)

java zip bytearray

5
推荐指数
1
解决办法
1万
查看次数

请解释为什么24,16,8用于将int转换为字节?

以下代码是将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)

java byte integer

3
推荐指数
1
解决办法
2436
查看次数

标签 统计

java ×2

byte ×1

bytearray ×1

integer ×1

zip ×1