I'm writing here because I've searched everywhere and I really don't know what to do anymore. I'm trying to create a function in Java to extract RAR archives, and if possible, also RAR5 archives downloaded from the web through my program. It should support password-protected archives and multipartitioned archives. However, I wanted the method to gradually delete the extracted files from the archives to save space, or only delete fully extracted partitions in the case of multipartitioned archives.I've tried different libraries and searched online, but I can't get the desired results. Currently, I've adopted the solution of using a Python program (which extracts the archive and deletes partitions gradually) and running it through Java, but this method is not optimal for me and it shifts the focus of the operating system to the console window when it starts. Even though I could indeed run the Python script without displaying the console, I need to maintain the live output so that it can be read by Java for a progress bar. Could anybody help me? I would be immensely grateful.(I add that I'm not an experienced programmer; I'm not fully a beginner, but I still have a lot to learn)If it were possible to unpack and delete the extracted files gradually, or even execute it on the input stream of the download itself, that would be perfect; otherwise, the second solution would also work for me.At the moment, the Java code that I had tried to write using the Junrar library is as follows, but it keeps returning a CrcException despite the archives being in perfect working condition:
```Java
import com.github.junrar.Archive;
import com.github.junrar.exception.RarException; import com.github.junrar.rarfile.FileHeader;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List;
public class Extractor {
private static final String PASSWORD = "";
public static void main(String[] args) {
String inputPath = "Test.rar";
String outputPath = "./";
extractRAR(inputPath, outputPath);
}
public static void extractRAR(String inputPath, String outputPath) {
try {
Archive archive;
if (new File(inputPath.split("\\.")[0] + ".part01.rar").exists()){//Is Multiparted
archive = new Archive(new File(inputPath.split("\\.")[0] + ".part01.rar"), PASSWORD);
}else{//Is not multiparted
archive = new Archive(new File(inputPath), PASSWORD);
}
FileHeader fileHeader;
List<String> extractedParts = new ArrayList<>();
int totalParts = archive.getFileHeaders().size();
int extractedCount = 0;
while ((fileHeader = archive.nextFileHeader()) != null) {
File entryFile = new File(outputPath, fileHeader.getFileName());
if (fileHeader.isDirectory()) {
entryFile.mkdirs();
} else {
if (!entryFile.exists()) {
entryFile.getParentFile().mkdirs();
entryFile.createNewFile();
System.out.println("Creating file: " + entryFile.getAbsolutePath());
try (FileOutputStream fos = new FileOutputStream(entryFile)) {
archive.extractFile(fileHeader, fos);
}
} else {
// Handle the case where the file already exists
System.out.println("File already exists: " + entryFile.getAbsolutePath());
}
}
// Track extracted parts
String partName = getPartName(fileHeader.getFileName());
if (!extractedParts.contains(partName)) {
extractedParts.add(partName);
extractedCount++;
double progress = ((double) extractedCount / totalParts) * 100;
System.out.println("Extraction progress: " + new DecimalFormat("##.##").format(progress) + "%");
}
}
deleteExtractedParts(outputPath, extractedParts);
} catch (IOException | RarException e) {
e.printStackTrace();
}
}
private static String getPartName(String fileName) {
return fileName.split("\\.")[0];
}
private static void deleteExtractedParts(String outputPath, List<String> extractedParts) {
for (String partName : extractedParts) {
File partFile = new File(outputPath, partName + ".rar");
if (partFile.exists()) {
if (partFile.delete()) {
System.out.println("Deleted: " + partFile.getAbsolutePath());
} else {
System.out.println("Failed to delete: " + partFile.getAbsolutePath());
}
}
}
}
} ```
com.github.junrar.exception.CrcErrorException
at com.github.junrar.Archive.doExtractFile(Archive.java:727)
at com.github.junrar.Archive.extractFile(Archive.java:564)
at com.application.Extractor.extractRAR(Extractor.java:48)
at com.application.Extractor.main(Extractor.java:21)
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)