/*
* Tencent is pleased to support the open source community by making Tinker available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taobao.common.dexpatcher.algorithms.diff.utils;
import com.taobao.common.dexpatcher.Configuration;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class FileOperation {
public static final boolean fileExists(String filePath) {
if (filePath == null) {
return false;
}
File file = new File(filePath);
if (file.exists()) {
return true;
}
return false;
}
public static final boolean deleteFile(String filePath) {
if (filePath == null) {
return true;
}
File file = new File(filePath);
if (file.exists()) {
return file.delete();
}
return true;
}
public static final boolean deleteFile(File file) {
if (file == null) {
return true;
}
if (file.exists()) {
return file.delete();
}
return true;
}
public static boolean isLegalFile(String path) {
if (path == null) {
return false;
}
File file = new File(path);
return file.exists() && file.isFile() && file.length() > 0;
}
public static long getFileSizes(File f) {
if (f == null) {
return 0;
}
long size = 0;
if (f.exists() && f.isFile()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
size = fis.available();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return size;
}
public static final boolean deleteDir(File file) {
if (file == null || (!file.exists())) {
return false;
}
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteDir(files[i]);
}
}
file.delete();
return true;
}
public static void cleanDir(File dir) {
if (dir.exists()) {
FileOperation.deleteDir(dir);
dir.mkdirs();
}
}
public static void copyResourceUsingStream(String name, File dest) throws IOException {
FileOutputStream os = null;
File parent = dest.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
InputStream is = null;
try {
is = FileOperation.class.getResourceAsStream("/" + name);
os = new FileOutputStream(dest, false);
byte[] buffer = new byte[TypedValue.BUFFER_SIZE];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
}
public static void copyFileUsingStream(File source, File dest) throws IOException {
FileInputStream is = null;
FileOutputStream os = null;
File parent = dest.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest, false);
byte[] buffer = new byte[TypedValue.BUFFER_SIZE];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
}
public static boolean checkDirectory(String dir) {
File dirObj = new File(dir);
deleteDir(dirObj);
if (!dirObj.exists()) {
dirObj.mkdirs();
}
return true;
}
/**
* zip list of file
*
* @param resFileList file(dir) list
* @param zipFile output zip file
* @throws IOException
*/
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), TypedValue.BUFFER_SIZE));
for (File resFile : resFileList) {
if (resFile.exists()) {
zipFile(resFile, zipout, "");
}
}
zipout.close();
}
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
final byte[] fileContents = readContents(resFile);
//linux format!!
if (rootpath.contains("\\")) {
rootpath = rootpath.replace("\\", "/");
}
ZipEntry entry = new ZipEntry(rootpath);
// if (compressMethod == ZipEntry.DEFLATED) {
entry.setMethod(ZipEntry.DEFLATED);
// } else {
// entry.setMethod(ZipEntry.STORED);
// entry.setSize(fileContents.length);
// final CRC32 checksumCalculator = new CRC32();
// checksumCalculator.update(fileContents);
// entry.setCrc(checksumCalculator.getValue());
// }
zipout.putNextEntry(entry);
zipout.write(fileContents);
zipout.flush();
zipout.closeEntry();
}
}
private static byte[] readContents(final File file) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final int bufferSize = TypedValue.BUFFER_SIZE;
try {
final FileInputStream in = new FileInputStream(file);
final BufferedInputStream bIn = new BufferedInputStream(in);
int length;
byte[] buffer = new byte[bufferSize];
byte[] bufferCopy;
while ((length = bIn.read(buffer, 0, bufferSize)) != -1) {
bufferCopy = new byte[length];
System.arraycopy(buffer, 0, bufferCopy, 0, length);
output.write(bufferCopy);
}
bIn.close();
} finally {
output.close();
}
return output.toByteArray();
}
public static long getFileCrc32(File file) throws IOException {
InputStream inputStream = new FileInputStream(file);
CRC32 crc = new CRC32();
int cnt;
while ((cnt = inputStream.read()) != -1) {
crc.update(cnt);
}
return crc.getValue();
}
public static String getZipEntryCrc(File file, String entryName) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
return null;
}
return String.valueOf(entry.getCrc());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static String getZipEntryMd5(File file, String entryName) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
return null;
}
return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void zipInputDir(File inputDir, File outputFile) throws IOException {
File[] unzipFiles = inputDir.listFiles();
List<File> collectFiles = new ArrayList<File>();
for (File f : unzipFiles) {
collectFiles.add(f);
}
FileOperation.zipFiles(collectFiles, outputFile);
}
public static boolean sevenZipInputDir(File inputDir, File outputFile, Configuration config) {
String outPath = inputDir.getAbsolutePath();
String path = outPath + File.separator + "*";
String cmd = config.mSevenZipPath;
ProcessBuilder pb = new ProcessBuilder(cmd, "a", "-tzip", outputFile.getAbsolutePath(), path, "-mx9");
Process pro;
try {
pro = pb.start();
InputStreamReader ir = new InputStreamReader(pro.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
while (input.readLine() != null) {
}
//destroy the stream
pro.waitFor();
pro.destroy();
} catch (IOException e) {
// e.printStackTrace();
FileOperation.deleteFile(outputFile);
// Logger.e("7a patch file failed, you should set the zipArtifact, or set the path directly");
return false;
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
}