Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:00:08+00:00 2026-05-22T20:00:08+00:00

I am trying to write a java ZIP util class as below: package fdbank.util;

  • 0

I am trying to write a java ZIP util class as below:

package fdbank.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 解压缩工具类
 * @author ggfan@amarsoft
 *
 */
public class ZIPUtil {

    private static void zip(File[] files, String dest) throws IOException{
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("dest")));
        for(File file : files){
            zip(file, zos);
        }
        zos.close();
    }

    private static void zip(File file, ZipOutputStream zos) throws IOException{
        byte[] buf = new byte[2048];
        @SuppressWarnings("unused")
        int bytes = 0;
        if(file.isDirectory()){
            ZipEntry entry = new ZipEntry(file.getName());
            zos.putNextEntry(entry);
            for(File subFile : file.listFiles()){
                zip(subFile, zos);
            }
            zos.closeEntry();
        }
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.getName());
        ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);
        while((bytes = fis.read(buf)) != -1){
            zos.write(buf);
        }
        zos.closeEntry();
        fis.close();
    }



    public static void compress(int archiveType, File[] files, String dest){

    }

    public static void main(String[] args){
        try {
            System.out.println("gan !!!!");
            zip(new File[]{new File("F:\\ziptest\\1.bmp")},"c:\\ziptest.zip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I run it ,no error but the zip file not created at all!!!
what’s wrong with my code ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:00:09+00:00Added an answer on May 22, 2026 at 8:00 pm

    You’re always writing to a file called “dest” and ignore the String parameter called dest (with the value c:\ziptest.zip).

    Replace "dest" with dest on the first line of your first zip() method.

    Also: you must not ignore the return value of fis.read(): If read() doesn’t fill the buffer buf, then you must tell that to the corresponding write() call:

    while((bytes = fis.read(buf)) != -1){
        zos.write(buf, 0, bytes);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a Java class to extract a large zip file
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
While trying to zip an archive using the java.util.zip I ran into a lot
I'm trying to write a Java function that takes a Class<?> and returns a
I'm trying to write a quick little java application to read the contents of
I'm trying to write some PHP to upload a file to a folder on
I'm having a problem trying to serve a zip file in a JSP. The
I'm trying write a java program to send live microphone data over UDP, then
I am trying to write a Java applet that will read from a user's
I'm trying to write a Java method to preform a multi-pop off a stack.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.