读取本地zip文件,并将文件中部分文件取出存为新的zip文件
public static void main(String[] args) throws Exception{
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\data\\tmp\\theme_apk.zip"));
String outFilePath = "D:\\data\\tmp\\sub.zip";
ZipInputStream zis = new ZipInputStream(bufferedInputStream);
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
BufferedReader br = new BufferedReader( new InputStreamReader(zis));
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFilePath));
while((zipEntry = zis.getNextEntry())!=null){
System.out.println("zip name: " + zipEntry.getName());
if( zipEntry.getName().contains("drawable-xxhdpi") ){
zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
int len = -1;
while ((len=zis.read(buffer)) != -1){
zos.write(buffer, 0, len);
}
}
zipEntry = null;
}
zos.close();
zis.closeEntry();
zis.close();
}
public void refreshFile() throws Exception{
RestTemplate restTemplate = new RestTemplate();
String path = "https://cdn-theme-eu-central-1.shalltry.com/rlktheme/theme/1680003127198.theme";
URI uri = new URI(path);
ResponseEntity<org.springframework.core.io.Resource> forEntity = restTemplate.getForEntity(path, org.springframework.core.io.Resource.class);
ZipInputStream zis = new ZipInputStream(forEntity.getBody().getInputStream());
String outFilePath = "D:\\data\\tmp\\test.zip";
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream);
while((zipEntry = zis.getNextEntry())!=null){
// System.out.println("zip name: " + zipEntry.getName());
if( zipEntry.getName().contains("drawable-xxhdpi") ){
zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
int len = -1;
while ((len=zis.read(buffer)) != -1){
zos.write(buffer, 0, len);
}
zos.closeEntry();
zos.flush();
}
zipEntry = null;
}
zis.close();
zos.flush();
byteArrayOutputStream.close();
zos.close();
//存储到OSS
System.out.println("size : " + byteArrayOutputStream.size());
Map<String, Object> result = fileManagerServer.fileUpload("dev", new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), "text/plain", "test.zip");
System.out.println(result.get("url"));
//存储到本地文件
/* InputStreamSource inputStreamSource = new ByteArrayResource(byteArrayOutputStream.toByteArray());
ZipInputStream zipInputStream = new ZipInputStream(inputStreamSource.getInputStream());
FileOutputStream fos = new FileOutputStream(outFilePath);
ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
byte[] bytes = new byte[1024];
int len = -1;
while((zipEntry = zipInputStream.getNextEntry()) != null){
zipOutputStream.putNextEntry(new ZipEntry(zipEntry.getName()));
while((len= zipInputStream.read(bytes)) !=-1){
zipOutputStream.write(bytes, 0, len);
}
zipOutputStream.closeEntry();
zipOutputStream.flush();
}
zipInputStream.close();
zipOutputStream.close();
fos.close();*/
}
public static void reUpload() throws Exception {
FileItemFactory factory = new DiskFileItemFactory(16,null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE,true,"fileName");
RestTemplate restTemplate = new RestTemplate();
String path = "https://cdn-theme-eu-central-1.shalltry.com/rlktheme/theme/1680003127198.theme";
URI uri = new URI(path);
ResponseEntity<org.springframework.core.io.Resource> forEntity = restTemplate.getForEntity(path, org.springframework.core.io.Resource.class);
ZipInputStream zis = new ZipInputStream(forEntity.getBody().getInputStream());
String outFilePath = "D:\\data\\tmp\\test.zip";
ZipEntry zipEntry;
byte[] buffer = new byte[102400000];
ZipOutputStream zos = new ZipOutputStream(item.getOutputStream());
while((zipEntry = zis.getNextEntry())!=null){
System.out.println("zip name: " + zipEntry.getName());
if( zipEntry.getName().contains("drawable-xxhdpi") ){
zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
int len = -1;
while ((len=zis.read(buffer)) != -1){
zos.write(buffer, 0, len);
}
}
zipEntry = null;
}
zos.flush();
FileOutputStream fos = new FileOutputStream(new File(outFilePath));
DataInputStream dis = new DataInputStream(item.getInputStream());
byte[] bytes = new byte[1024];
int len = -1;
while((len= dis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
fos.close();
dis.close();
zos.close();
zis.closeEntry();
zis.close();
}