Java file archiving and restore

This example shares the specific code of file archiving and restoration in Java for your reference. The specific contents are as follows

Basic idea:

File archiving, in other words, is to save the bytes of multiple files into one file. Therefore, we must define the storage format to extract the files from the package again.

The file consists of the file name and content. To restore the file completely, we must save these two things at the same time, and we don't know the byte size of the file name and content. Therefore, we must use a fixed size space to store their size.

Storage format

code implementation

Because the file content size is 4 bytes (i.e. int type), if we want to save it to the file, we need to convert it into a byte array. We specify that the low order is in the front and the high order is in the back. The conversion operation is encapsulated in this format and a basic class datautil.

Datautil code

package util;

public class DataUtil {
 public static byte[] int2bytes(int src) {
 byte[] rt = new byte[4];
 for(int i=0; i<4; ++i) {
 rt[i] = (byte)(src>>(i*8));
 }
 return rt;
 }
 public static int bytes2int(byte[] src) {
 int rt = 0;
 for(int i=0; i<4; ++i) {
 rt |= (src[i]&0xFF)<<(i*8);
 //字节在进行移位运算时,首先会被转换成int类型,
 //此时若字节的符号位为1,它前面就会补全1,比如:
 //0x80在byte类型时是-128,而转换成int,它的值还是
 //-128,即0xffffff80,而我们移位运算想要的是
 //0x00000080,即前面补全0,跟我们拆时一致。为此,
 //我们让它与0xFF相与,从0xffffff80变为0x00000080。
 }
 return rt;
 }
}

Archive class

package wfb.binSama;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import util.DataUtil;

/**
 * @author binSama
 */
public class Archive {

 public static void archive(File[] srcs,File tar) {//归档
 try {
 FileOutputStream fos = new FileOutputStream(tar);
 for(int i=0; i<srcs.length; ++i) {
 //获得文件名
 byte[] fileName = srcs[i].getName().getBytes();
 //获得文件名长度
 byte fileNameLen = (byte)fileName.length;
 //获得文件内容
 FileInputStream fis = new FileInputStream(srcs[i]);
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 int len = -1;
 byte[] buf = new byte[1024];
 while((len = fis.read(buf)) != -1) {
  baos.write(buf,len);
 }
 baos.close();
 fis.close();
 byte[] fileContent = baos.toByteArray();
 //获得文件内容长度
 byte[] fileContentLen = DataUtil.int2bytes(fileContent.length);
 //写入
 fos.write(fileNameLen);
 fos.write(fileName);
 fos.write(fileContentLen);
 fos.write(fileContent);
 }
 fos.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 public static void unArchive(File src) {//解档到当前文件夹
 try {
 FileInputStream fis = new FileInputStream(src);
 int fileNameLen = -1;
 while((fileNameLen = fis.read()) != -1){
 byte[] byteFileName = new byte[fileNameLen];
 fis.read(byteFileName);
 String fileName = src.getParent() +"\\"+ new String(byteFileName);
 FileOutputStream fos = new FileOutputStream(fileName);
 byte[] byteFileContentLen = new byte[4];
 fis.read(byteFileContentLen);
 int fileContentLen = DataUtil.bytes2int(byteFileContentLen);
 int divisorFileContentLen = fileContentLen / 1024;
 int remainderFileContentLen = fileContentLen % 1024;
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 byte[] divisorBuf = new byte[1024];
 for(int i=0; i<divisorFileContentLen; ++i) {
  fis.read(divisorBuf);
  baos.write(divisorBuf);
 }
 byte[] remainderBuf = new byte[remainderFileContentLen];
 fis.read(remainderBuf);
 baos.write(remainderBuf);
 baos.close();
 byte[] fileContent = baos.toByteArray();
 fos.write(fileContent);
 }
 fis.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
}

Test:

package wfb.binSama;

import java.io.File;

public class Test {
 @org.junit.Test
 public void test() {
 File[] files = new File[3];
 files[0] = new File("E:\\waster\\Archiver1\\1.txt");
 files[1] = new File("E:\\waster\\Archiver1\\2.png");
 files[2] = new File("E:\\waster\\Archiver1\\3.txt");
 File bsm = new File("E:\\waster\\Archiver2\\archive.bsm");
 Archive.archive(files,bsm);
 Archive.unArchive(bsm);
 }

}

phenomenon

Successfully generated archive in E: \ waster \ archiver2 folder BSM files and unpacks 1 txt 2. png 3. Txt three files.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>