Skip to content

Commit

Permalink
百度云解压修复算法bug修改,Central Size为4字节
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Mar 14, 2018
1 parent 6942f93 commit 2a00d2f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 83 deletions.
50 changes: 9 additions & 41 deletions common/src/main/java/lee/study/down/io/BdyZip.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,18 @@ private static List<String> getCentralList(FileChannel fileChannel)
throws IOException, IllegalArgumentException {
List<String> centralList = new ArrayList<>();
//read EOCD
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
ByteBuffer byteBuffer = ByteBuffer.allocate(6);
fileChannel.position(fileChannel.size() - EOCD_SIZE + CENTRAL_COUNT_OFFSET);
fileChannel.read(byteBuffer);
byteBuffer.flip();
byte[] bts2 = new byte[2];
//offset 10
//offset:10 bytes:2
byteBuffer.get(bts2);
int centralCount = (int) ByteUtil.btsToNumForSmall(bts2);
//offset 12
byteBuffer.get(bts2);
long centralSize = ByteUtil.btsToNumForSmall(bts2);
//offset 12 bytes:4
byte[] bts4 = new byte[4];
byteBuffer.get(bts4);
long centralSize = ByteUtil.btsToNumForSmall(bts4);
fileChannel.position(fileChannel.size() - centralSize - EOCD_SIZE);
//read Central directory list
ByteBuffer centralBuffer = ByteBuffer.allocate(CENTRAL_SIZE);
Expand Down Expand Up @@ -254,7 +255,7 @@ public static void unzip(String path, String toPath, BdyUnzipCallback callback)
if (callback != null) {
callback.onEntryStart(zipEntry);
}
/*long fileSize = zipEntry.getCompressedSize();
long fileSize = zipEntry.getCompressedSize();
if (zipEntry.isDir()) {
FileUtil.createDirSmart(toPath + File.separator + zipEntry.getFileName());
if (callback != null) {
Expand All @@ -276,7 +277,7 @@ public static void unzip(String path, String toPath, BdyUnzipCallback callback)
}
}
unzipChannel.close();
}*/
}
}
if (callback != null) {
callback.onDone();
Expand All @@ -297,42 +298,9 @@ public static void unzip(String path, String toPath)
}

public static void main(String[] args) throws Exception {
// unzip(args[0], args[1], new TestUnzipCallback());
unzip("f:/down/pack13.zip", "f:/down/【批量下载】幻月完整开区端等", null);
/*
77b67000
377b67000
*/
/*System.out.println(Long.toHexString(0x377b67000L - 0x77b67000L));
System.out.println(
Long.toHexString(0x77b67000L + 0xFFFFFFFFL + 1 + 0xFFFFFFFFL + 1 + 0xFFFFFFFFL + 1));
System.out.println(_4G);
System.out.println(0xFFFFFFFFL);*/
unzip(args[0], args[1], new TestUnzipCallback());
}

private static int printBuffer(ByteBuffer byteBuffer, int[] array) {
int offset = 0;
int n = 0;
int m = 0;
int k = 0;
for (int size : array) {
byte[] bts = new byte[size];
byteBuffer.get(bts);
if (offset == 42) {
}
if (offset == 28) {
n = (int) ByteUtil.btsToNumForSmall(bts);
}
if (offset == 30) {
m = (int) ByteUtil.btsToNumForSmall(bts);
}
if (offset == 32) {
k = (int) ByteUtil.btsToNumForSmall(bts);
}
offset += size;
}
return n + m + k;
}

/**
* 检查是否为百度云合并下载ZIP
Expand Down
55 changes: 13 additions & 42 deletions common/src/main/java/lee/study/down/util/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.netty.buffer.ByteBuf;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -17,19 +16,14 @@
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.cert.X509Certificate;
import java.util.UUID;
import lee.study.down.constant.HttpDownStatus;
import lee.study.down.model.ChunkInfo;
import lee.study.down.model.TaskInfo;

public class ByteUtil {

/**
* 大端序
*/
public static byte[] longToBtsForBig(long num) {
//long 8字节
byte[] bts = new byte[8];
public static byte[] numToBtsForBig(long num, int len) {
byte[] bts = new byte[len];
for (int i = 0; i < bts.length; i++) {
bts[bts.length - i - 1] = (byte) ((num >> 8 * i) & 0xFF);
}
Expand All @@ -39,7 +33,15 @@ public static byte[] longToBtsForBig(long num) {
/**
* 大端序
*/
public static long btsToLongForBig(byte[] bts) {
public static byte[] numToBtsForBig(long num) {
//long 8字节
return numToBtsForBig(num, 8);
}

/**
* 大端序
*/
public static long btsToNumForBig(byte[] bts) {
//int 4字节
long num = 0;
for (int i = 0; i < bts.length; i++) {
Expand Down Expand Up @@ -294,7 +296,8 @@ public static boolean matchToken(FileChannel fileChannel, long position, byte[]
return matchToken(fileChannel, -1, position, bts);
}

public static boolean matchToken(FileChannel fileChannel, long start, long position, byte[]... btsArr)
public static boolean matchToken(FileChannel fileChannel, long start, long position,
byte[]... btsArr)
throws IOException {
boolean ret;
ByteBuffer buffer = ByteBuffer.allocate(btsArr[0].length);
Expand All @@ -314,38 +317,6 @@ public static boolean matchToken(FileChannel fileChannel, long start, long posit
return ret;
}

public static void main(String[] args) throws IOException {
/*String file = args[0];
long totalSize = Long.parseLong(args[1]);*/
String file = "F:\\【批量下载】c .part7等.zip";
long totalSize = 12312;
File downFile = new File(file);
File[] chunks = new File(
downFile.getParentFile().getAbsolutePath() + File.separator + "." + downFile.getName()
+ "_cks").listFiles();
TaskInfo taskInfo = new TaskInfo()
.setId(UUID.randomUUID().toString())
.setFilePath(downFile.getParentFile().getAbsolutePath())
.setFileName(downFile.getName())
.setConnections(chunks.length)
.setSupportRange(true)
.setStatus(HttpDownStatus.RUNNING)
.setTotalSize(totalSize)
.setStartTime(System.currentTimeMillis())
.buildChunkInfoList();
long downSize = 0;
for (int i = 0; i < chunks.length; i++) {
downSize += chunks[i].length();
ChunkInfo chunkInfo = taskInfo.getChunkInfoList().get(i);
chunkInfo.setDownSize(chunks[i].length());
chunkInfo.setStatus(HttpDownStatus.RUNNING);
chunkInfo.setStartTime(System.currentTimeMillis());
chunkInfo.setIndex(i);
}
taskInfo.setDownSize(downSize);
serialize(taskInfo, taskInfo.buildTaskRecordFilePath(), taskInfo.buildTaskRecordBakFilePath());
}

public static String getCertHash(X509Certificate certificate) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] der = certificate.getEncoded();
Expand Down

0 comments on commit 2a00d2f

Please sign in to comment.