Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want the ability to rename a file name in a zip file #72

Closed
l54808821 opened this issue Sep 27, 2019 · 5 comments
Closed

I want the ability to rename a file name in a zip file #72

l54808821 opened this issue Sep 27, 2019 · 5 comments
Assignees
Labels
new-feature New feature or request resolved

Comments

@l54808821
Copy link

I want the ability to rename a file name in a zip file
If I use the following code, I will corrupt the original file

    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setFileNameInZip("jmeterTemplate/data/result1.csv");
    zipParameters.setSourceExternalStream(true);

    ZipFile zipFile = new ZipFile("D:\\aaaa\\jmeterTemplate.zip");
    zipFile.setFileNameCharset("GBK");
    FileHeader fileHeader1 = zipFile.getFileHeader("jmeterTemplate/data/result.csv");
    ZipInputStream inputStream = zipFile.getInputStream(fileHeader1);
    zipFile.addStream(inputStream,zipParameters);
@LeeYoung624
Copy link
Contributor

I guess you are using a old version of zip4j.
I tested with the latest code, and I think this could be a workaround for you:

    ZipParameters parameters = new ZipParameters();
    parameters.setFileNameInZip("newFileName");

    ZipFile resource = new ZipFile("/home/temp.zip");
    FileHeader fileHeader = resource.getFileHeader("fileToBeRenamed");
    InputStream is = resource.getInputStream(fileHeader);

    resource.addStream(is, parameters);
    resource.removeFile("fileToBeRenamed");

I tested on Ubuntu and it works well. But it will throw exception on Windows. Hope this helps you.

@l54808821
Copy link
Author

public static void renameFile(String zipPathName, String oldName, String newName) throws Exception {
    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setFileNameInZip(newName);
    zipParameters.setSourceExternalStream(true);

    ZipFile zipFile = new ZipFile(zipPathName);
    zipFile.setFileNameCharset("GBK");
    FileHeader fileHeader = zipFile.getFileHeader(oldName);
    if (fileHeader != null) {
        ZipInputStream inputStream = zipFile.getInputStream(fileHeader);
        InputStream bufferedInputStream = IOUtils.toBufferedInputStream(inputStream);
        zipFile.addStream(bufferedInputStream, zipParameters);
        inputStream.close();
        bufferedInputStream.close();
        removeFile(zipFile,oldName);
    }
}

@srikanth-lingala srikanth-lingala self-assigned this Sep 29, 2019
@srikanth-lingala srikanth-lingala added the new-feature New feature or request label Sep 29, 2019
@srikanth-lingala
Copy link
Owner

This is a good idea for a new feature in zip4j. But it is slightly complicated to implement. I could not integrate in into v2.2.1 which was released today. But I will include this feature in the next release of zip4j.

@srikanth-lingala
Copy link
Owner

Feature added to zip4j. It is now possible to rename file(s) in a zip file. Please look at the documentation in README under the section "Rename files" for more info.

@srikanth-lingala
Copy link
Owner

Feature included in v2.4.0 released today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature New feature or request resolved
Projects
None yet
Development

No branches or pull requests

3 participants