最近做一个一键刻盘的功能,需要先将刻盘的文件封装成iso文件。经过一系列的资料查询找到了相关解决方案。下面直接上代码
要想成功运行下面代码需要先导入依赖的jar包
依赖包下载地址
封装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