基于Java生成ISO文件或iso9660文件

最近做一个一键刻盘的功能,需要先将刻盘的文件封装成iso文件。经过一系列的资料查询找到了相关解决方案。下面直接上代码

要想成功运行下面代码需要先导入依赖的jar包

依赖包下载地址

基于Java封装iso文件依赖的jar资源-CSDN文库

封装iso文件代码如下

public class IsoCreation {
    ISO9660RootDirectory root;
    ISO9660Config iso9660config;
    RockRidgeConfig rockConfig;
    JolietConfig jolietConfig;
    ElToritoConfig elToritoConfig;

    // This is a conversion of https://github.com/danveloper/provisioning-gradle-plugin/blob/
    // master/src/main/groovy/gradle/plugins/provisioning/tasks/image/ImageAssemblyTask.groovy
    public IsoCreation() {
        root = new ISO9660RootDirectory();
    }

    public void insertFile(File passedInsert) {
        try {
            root.addContentsRecursively(passedInsert);
        } catch (HandlerException e) {
            e.printStackTrace();
        }
    }

    public ISO9660Config setIsoConfig(String volumeId) {
        //Let's make a new configuration for the basic file system
        iso9660config = new ISO9660Config();
        iso9660config.allowASCII(false);
        //The standard says you are not allowed more than 8 levels, unless you enable RockRidge support
        iso9660config.restrictDirDepthTo8(true);
        //Set the volume ID
        try {
            iso9660config.setVolumeID(volumeId.substring(0, Math.min(volumeId.length(), 15)));
        } catch (ConfigException e) {
            e.printStackTrace();
        }
        iso9660config.forceDotDelimiter(false);
        try {
            iso9660config.setInterchangeLevel(3);
        } catch (ConfigException e) {
            e.printStackTrace();
        }

        return iso9660config;
    }

    public RockRidgeConfig setRockConfig() {
        rockConfig = new RockRidgeConfig();
        rockConfig.setMkisofsCompatibility(false);
        rockConfig.hideMovedDirectoriesStore(true);
        rockConfig.forcePortableFilenameCharacterSet(true);
        return rockConfig;
    }

    public JolietConfig setJolietConfig(String volumeId) {
        jolietConfig = new JolietConfig();
        try {
            //Not sure if there is a limit here, but it seems most names are fine
            jolietConfig.setVolumeID(volumeId);
        } catch (ConfigException e) {
            e.printStackTrace();
        }
        jolietConfig.forceDotDelimiter(false);
        return jolietConfig;
    }

    /**
     * This is for boot information on the iso
     *
     * @return The finalized config.
     */
    public ElToritoConfig setElToritoConfig(){
        elToritoConfig = null;
        return elToritoConfig;
    }

    /**
     * Close out the disc.
     * @param isoPath
     */
    public void finalizeDisc(File isoPath) {
        ISOImageFileHandler handler = null;
        try {
            handler = new ISOImageFileHandler(isoPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            CreateISO iso = new CreateISO(handler, root);
            try {
                iso.process(iso9660config, rockConfig, jolietConfig, elToritoConfig);

            } catch (HandlerException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

	public static void createIso(String inputFileDir, String outIsoPath) {
	        //Create a new root of our disc
	        IsoCreation sampleCreation = new IsoCreation();
	        //Add our files, you have to give a directory to use the recusively adding function
	        sampleCreation.insertFile(new File(inputFileDir)); // 设置要封装成iso文件的文件夹
	        //This is the base iso9660 standard file system
	        sampleCreation.setIsoConfig("isotest");
	        //To quote wikipedia "Rock Ridge is an extension to the ISO 9660 volume format, commonly used on CD-ROM and
	        // DVD media, which adds POSIX file system semantics."
	//        sampleCreation.setRockConfig();
	        //This is another extension to the standard after Windows 95 to add support for longer filenames
	        sampleCreation.setJolietConfig("isotest");
	        //El Torito is boot information for the disc
	//        sampleCreation.setElToritoConfig();
	        //Finalize and save our ISO
	        sampleCreation.finalizeDisc(new File(outIsoPath)); // 输出到目标iso文件
	    }
	public static void main(String[] args) {
		createIso("E:/isotest", "D:/test.iso"); // 调用
	}
}

读取iso文件代码


/***
 * This is a adaptation of https://github.com/danveloper/provisioning-gradle-plugin/blob/master/src/main/groovy/
 * gradle/plugins/provisioning/tasks/image/ImageAssemblyTask.groovy
 */

public class ReadIso {
    Iso9660FileSystem discFs;

    public ReadIso(File isoToRead, File saveLocation) {

        try {
            //Give the file and mention if this is treated as a read only file.
            discFs = new Iso9660FileSystem(isoToRead, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Make our saving folder if it does not exist
        if (!saveLocation.exists()) {
            saveLocation.mkdirs();
        }

        //Go through each file on the disc and save it.
        for (Iso9660FileEntry singleFile : discFs) {
            if (singleFile.isDirectory()) {
                new File(saveLocation, singleFile.getPath()).mkdirs();
            } else {
                File tempFile = new File(saveLocation, singleFile.getPath());
                try {
                    //This is java 7, sorry if that is too new for some people
                    Files.copy(discFs.getInputStream(singleFile), tempFile.toPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
    	ReadIso tempIso = new ReadIso(new File("E:\\1exe\\test\\1.iso"), new File("E:\\1exe\\test\\"));
	}
}

以上可成功封装iso,读取iso

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

档案小唐总

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值