livp图片是一个zip格式的压缩包,更改文件的拓展名解压一样可以提取。
思路用Java把livp变成zip,然后进行解压。其他代码应该也可以实现,编程水平比较菜仅供参考。
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* Description: iPhone live格式图片提取
*
*/
public class LivpMain {
static final String PATH = "D:\\BaiduNetdiskDownload\\临时照片\\来自:iPhone";
static final String SUFFIX = "livp";
static final String ZIP = "zip";
static final String EXPAND = ".";
static final String HEIC = "heic";
static final String MOV = "mov";
static final String JPEG = "jpeg";
public static void main(String[] args) throws IOException {
// toZip();
// unzip();
// toRootPath();
// delDirectory();
delZip();
}
/**
* 删除多余zip
*/
public static void delZip() {
File baseDir = new File(PATH);
File[] files = baseDir.listFiles();
for (File file : files) {
if (!file.isDirectory()) {
String fileName = file.getName();
int lastIndexOf = fileName.lastIndexOf(EXPAND);
String prefix = fileName.substring(0, lastIndexOf);
String suffix = fileName.substring(lastIndexOf + 1);
if (ZIP.equals(suffix)) {
file.delete();
}
}
}
}
/**
* 删除多余目录
*/
public static void delDirectory() {
File baseDir = new File(PATH);
File[] files = baseDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
File[] listedFiles = file.listFiles();
if (listedFiles.length == 0) {
file.delete();
}
}
}
}
/**
* 重命名文件复制到根目录
*
* @throws IOException
*/
public static void toRootPath() throws IOException {
File baseDir = new File(PATH);
File[] files = baseDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
for (File listFile : file.listFiles()) {
int lastIndexOf = listFile.getName().lastIndexOf(EXPAND);
String suffix = listFile.getName().substring(lastIndexOf + 1);
String prefix = listFile.getName().substring(0, lastIndexOf);
File newFile = null;
if (HEIC.equals(suffix)) {
newFile = new File(PATH + File.separator + file.getName() + EXPAND + HEIC);
} else if (MOV.equals(suffix)) {
newFile = new File(PATH + File.separator + file.getName() + EXPAND + MOV);
} else if (JPEG.equals(suffix)) {
newFile = new File(PATH + File.separator + file.getName() + EXPAND + JPEG);
} else {
continue;
}
if (!newFile.exists()) {
listFile.renameTo(newFile);
}
}
}
}
}
/**
* 解压文件
*
* @throws IOException
*/
public static void unzip(File file) throws IOException {
String fileName = file.getName();
int lastIndexOf = fileName.lastIndexOf(EXPAND);
if (file.isDirectory()) {
return;
}
String prefix = fileName.substring(0, lastIndexOf);
String suffix = fileName.substring(lastIndexOf + 1);
// 解压文件
if (ZIP.equals(suffix)) {
// 待解压的zip文件,需要在zip文件上构建输入流,读取数据到Java中
File outFile = null; // 输出文件的时候要有文件夹的操作
ZipFile zipFile = new ZipFile(file); // 实例化ZipFile对象
ZipInputStream zipInput = null; // 定义压缩输入流
// 定义解压的文件名
OutputStream out = null; // 定义输出流,用于输出每一个实体内容
InputStream input = null; // 定义输入流,读取每一个ZipEntry
ZipEntry entry = null; // 每一个压缩实体
zipInput = new ZipInputStream(new FileInputStream(file));
while ((entry = zipInput.getNextEntry()) != null) {
File dir = new File(PATH + File.separator + prefix);
if (!dir.exists()) {
dir.mkdir();
}
outFile = new File(PATH + File.separator + prefix + File.separator + entry.getName());
if (!outFile.getParentFile().exists()) { // 如果输出文件夹不存在
outFile.getParentFile().mkdirs();
// 创建文件夹 ,如果这里的有多级文件夹不存在,请使用mkdirs()
// 如果只是单纯的一级文件夹,使用mkdir()就好了
}
if (!outFile.exists()) { // 判断输出文件是否存在
if (entry.isDirectory()) {
outFile.mkdirs();
System.out.println("create directory...");
} else {
outFile.createNewFile(); // 创建文件
System.out.println("create file...");
}
}
if (!entry.isDirectory()) {
input = zipFile.getInputStream(entry); // 得到每一个实体的输入流
out = new FileOutputStream(outFile); // 实例化文件输出流
int temp = 0;
while ((temp = input.read()) != -1) {
out.write(temp);
}
input.close(); // 关闭输入流
out.close(); // 关闭输出流
}
}
if (input != null) input.close();
}
}
/**
* 解压文件
*
* @throws IOException
*/
public static void unzip() throws IOException {
File baseDir = new File(PATH);
File[] files = baseDir.listFiles();
for (File file : files) {
new Thread(() -> {
try {
unzip(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).start();
// unzip(file);
}
}
/**
* livp -> zip
*/
public static void toZip() {
File baseDir = new File(PATH);
File[] files = baseDir.listFiles();
for (File file : files) {
String fileName = file.getName();
int lastIndexOf = fileName.lastIndexOf(EXPAND);
// 跳过目录
if (lastIndexOf == -1) {
continue;
}
String prefix = fileName.substring(0, lastIndexOf);
String suffix = fileName.substring(lastIndexOf + 1);
// 重命名文件
if (SUFFIX.equals(suffix)) {
File newFile = new File(PATH + File.separator + prefix + EXPAND + ZIP);
if (!newFile.exists()) {
file.renameTo(newFile);
}
}
}
}
}