LathaPatil
LathaPatil

Reputation: 143

7-zip-JBinding usage to unzip a file

I have used below java code to unzip a .gz file. The .gz file contains a folder/textFile. It is working fine only if there is some data in the text file.I need it to work even if there is no data in text file. Help me with any modifications in the below java code.

public static void main(String[] args) {
    String sourceZipFile = "C:/SKKIKRFULL20151014.gz";
    final String destinationDir = "C:/";
    RandomAccessFile randomAccessFile = null;
    ISevenZipInArchive inArchive = null;
    try {
        randomAccessFile = new RandomAccessFile(sourceZipFile, "r");
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                new RandomAccessFileInStream(randomAccessFile));

        // Getting simple interface of the archive inArchive
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

        for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {

            final int[] hash = new int[] { 0 };

            if (!item.isFolder()) {

                ExtractOperationResult result;
                result = item.extractSlow(new ISequentialOutStream() {

                    public int write(final byte[] data) throws SevenZipException {
                        try {

                            if (item.getPath().indexOf(File.separator) > 0) {

                                String path = destinationDir + File.separator
                                        + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));

                                File folderExisting = new File(path);

                                if (!folderExisting.exists()) {

                                    new File(path).mkdirs();
                                }
                            }
                            OutputStream out = new FileOutputStream(
                                    destinationDir + File.separator + item.getPath());

                            out.write(data);
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hash[0] |= Arrays.hashCode(data);
                        return data.length; // Return amount of proceed data
                    }
                }); 
                if (result == ExtractOperationResult.OK) {
                    System.out.println(String.format("%9X | %s", hash[0], item.getPath()));
                } else {
                    System.err.println("Error extracting item: " + result);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
                e.printStackTrace();
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
                e.printStackTrace();
            }
        }
    }
}

As i debugged it. It is not entering the below block.

 result = item.extractSlow(new ISequentialOutStream() {//write() method})

Wanted to know How can i modify the code to make it work.

Upvotes: 0

Views: 1069

Answers (1)

Gernot Krost
Gernot Krost

Reputation: 1124

Your code only creates a file if its payload is larger than 0 bytes. I believe that's because ISequentialOutStream write(byte[] data) - method only writes if the array is larger than 0 bytes. From the javadocs:

Write data byte array to the stream. If data.length > 0 this function must write at least 1 byte.

You should change the write() - methods code to allow empty file creation. Maybe this helps: Stackoverflow: decompress files with .7z extension in java

Upvotes: 2

Related Questions